Find the second and fourth Saturday of the month

How to find the second and fourth Saturday of the month. I wrote the following lines: -

echo "may 2nd sat ".date('d', strtotime('may 2013 second saturday')); echo '<br/>may 4th sat '.date('d', strtotime('may 2013 fourth saturday')); echo '<br/>june 2nd sat '.date('d', strtotime('june 2013 second saturday')); echo '<br/>june 4th sat '.date('d', strtotime('june 2013 fourth saturday')); 

It gives the following result: -

 may 2nd sat 11 may 4th sat 25 june 2nd sat 15 june 4th sat 29 

He gives the correct answer in the month of may , but not june 2013 , on jun 1013 2 and 4 Saturdays should be 8 and 22, respectively. How can I solve this problem.

+7
source share
5 answers

I'm not sure why yours won't work, but try this, it worked for me:

 echo '<br/>june 2nd sat '.date('d', strtotime('second sat of june 2013')); echo '<br/>june 4th sat '.date('d', strtotime('fourth sat of june 2013')); 

It is based on the example of "first sat of July 2008" in the PHP manual page.

+7
source

I do not know why this causes this error, as far as I found a solution, how to get the correct answers

 echo date('d',strtotime('+1 week sat may 2013')).'<BR>'; echo date('d',strtotime('+3 week sat may 2013')).'<BR>'; echo date('d',strtotime('+1 week sat june 2013')).'<BR>'; echo date('d',strtotime('+3 week sat june 2013')).'<BR>'; 

This solution works great and shows the correct results.

Output:

 11 25 08 22 
+4
source

Nowadays, my preferred method is to extend the PHP DateTime object : -__

 class MyDateTime extends DateTime { /** * Returns a MyDateTime object set to 00:00 hours on the nth occurence * of a given day of the month * * @param string $n nth day required, eg first, second etc * @param string $day Name of day * @param mixed $month Month number or name optional defaults to current month * @param mixed $year optional defaults to current year * * @return MyDateTime set to last day of month */ public function nthDayOfMonth($n, $day, $month = null, $year = null) { $timestr = "$n $day"; if(!$month) $month = $this->format('M'); $timestr .= " of $month $year"; $this->setTimestamp(strtotime($timestr)); $this->setTime(0, 0, 0); return $this; } } $dateTime = new MyDateTime(); echo $dateTime->nthDayOfMonth('second', 'Sun', 'Jul', 2011)->format('Ym-d'); 

Output: -

 2011-07-10 
+1
source

Then you must run a version of PHP below version 5.2.7, as specified manually

In PHP 5 to 5.2.7, a request for a given occurrence of a given day of the week in a month when this day of the week was the first day of the month will incorrectly add one week to the returned timestamp. This has been fixed in 5.2.7 and later.

So, if you can upgrade the version of PHP, which would be a better solution. Otherwise, you can check something like.

 <?php function showDay($month, $year, $day, $count) { $list = array(1=>'first',2=>'second',3=>'third',4=>'fourth',5=>'fifth'); $first = date('d', strtotime($month . ' ' . $year . ' ' . $list[1] .' '.$day)); $show= ($first>7) ? $count-1 : $count; return date('d', strtotime($month . ' ' . $year . ' ' . $list[$show] .' '.$day)); } echo '<br/>june 2nd sat '.showDay('june', 2013, 'saturday', 2); ?> 
+1
source

I usually do not rely on a piece of string. What about custom function?

 function getSaturdayDay($year, $month, $position) { $firstDay = date('w', mktime(0, 0, 0, $month, 1, $year)); $diff = 6 - $firstDay; return 1 + $diff + $position * 7; } 

and use it in your context

 echo "may 2nd sat " . getSaturdayDay(2013, 5, 1); echo '<br/>may 4th sat ' . getSaturdayDay(2013, 5, 3); echo '<br/>june 2nd sat ' . getSaturdayDay(2013, 6, 1); echo '<br/>june 4th sat ' . getSaturdayDay(2013, 6, 3); 
+1
source

All Articles