Reputation: 41
I have already found a solution to this problem, but it's getting wrong for some condition. Following are my findings:
This code segment is to print the first Saturday of August 2015 and July 2015:
$sat = date("F Y",strtotime("2015-08-01"));
echo $fir_sat = "$sat first saturday";
echo " is ".date('d', strtotime($fir_sat));
$sat = date("F Y",strtotime("2015-07-04"));
echo $fir_sat = "$sat first saturday";
echo " is ".date('d', strtotime($fir_sat));
Following are the output:
August 2015 first Saturday is 08
July 2015 first Saturday is 04
But actually it is 01:
August 2015 first Saturday is 01
How does it happen? What is the solution?
Based on your feedback's I have tried some coding. Following are my findings. The error happened because of php version.
In lower version of PHP like 5.2.9 the following code is not working
echo date('Y-m-d', strtotime('first saturday of august 2015'));
But in higher version like 5.4 it is wokring
Any ideas?
Upvotes: 4
Views: 3950
Reputation: 529
Simple Solution,tested in my local.
$sat = date("d F Y",strtotime("2015-08-01"));
echo $fir_sat = "$sat<br> First saturday";
echo " is ".date('d', ($fir_sat));
$thi_sat = strtotime ( '+14 day' , strtotime ( $sat ) ) ;
$thi_sat = date ( 'd' , $thi_sat );
echo "<br>third Saturday at ".$thi_sat;
$fifth_sat = strtotime ( '+28 day' , strtotime ( $sat ) ) ;
$fifth_sat = date ( 'd' , $fifth_sat );
echo "<br>Fifth Saturday at ".$fifth_sat;
Out put is:
01 August 2015
First saturday is 01
third Saturday at 15
Fifth Saturday at 29
This might help.Cheers!!
Upvotes: 0
Reputation: 1864
Try this
echo date('Y-m-d', strtotime('first saturday of august 2015'));
echo date('Y-m-d', strtotime('third saturday of august 2015'));
echo date('Y-m-d', strtotime('fifth saturday of august 2015'));
Upvotes: 0
Reputation: 21437
Try using of
as
echo date('d F Y', strtotime('first saturday of $fir_sat'));
"ordinal dayname 'of' " does not advance to another day.
instead of
echo date('d F Y', strtotime('$fir_sat first saturday'));
"ordinal dayname " does advance to another day.
You can check the difference over here
You can also check Docs(Notes)
Upvotes: 4
Reputation: 1380
This way you can utilize strtotime function.
echo date('d F Y', strtotime('first saturday of August 2015'));
echo "<br>";
echo date('d F Y', strtotime('second saturday of August 2015'));
echo "<br>";
echo date('d F Y', strtotime('fifth saturday of August 2015'));
Make sure you are passing proper year and month at the end, based on that it will give you correct date.
Thanks
Amit
Upvotes: 0