PHP Strtotime erratic function - php

The following code should take the fourth friday of February and the fourth friday of April and return it.
$datei1 = date(strtotime('fourth friday', strtotime('february', strtotime(date('01-m-Y')))));
$datei2 = date(strtotime('fourth friday', strtotime('april', strtotime(date('01-m-Y')))));
Its working but taking the 5th friday of April not the fourth. I can only assume that it does not believe the first of April counts.
Any ideas,
Marvellous

Your code is very complicated for no reason. Try this (PHP 5.3):
$date = date('Y-m-d', strtotime('fourth friday of april 2011'));
This will give you:
2011-04-22
In PHP 5.2, this syntax seems to work in all cases:
$date = date('Y-m-d', strtotime('fourth friday', strtotime('april - 1 second')));

strtotime('fourth friday', $now) probably really means "the fourth Friday relative to $now".
The $now parameter you give is 1st April 2011, which is a Friday. The fourth Friday after Fri 1st April is the fifth Friday of the month.

It appears you're using an older version of PHP. Major changes have been introduced to handle the edge case you're experiencing here. In which case I believe the following is the cleanest approach:
echo date('Y-m-d', strtotime('+4 fridays', mktime(0,0,0,4,0,date('Y'))));
// or
echo date('Y-m-d', strtotime('fourth friday', mktime(0,0,0,4,0,date('Y'))));
This should give you the fourth friday in april of the current year. I've tested successfully in 5.3 but you'll have to test on you older installation.

PHP Changelog ::
5.2.7 In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.

Related

PHP strtotime returning wrong result when looking for a "first saturday" of a given month [duplicate]

I'm working on a PHP function which calculates holidays:
function holidays($country = 1, $timespan_start = 0, $timespan_end = 0)
The holidays are returned as timestamps in an array.
Since I have to calculate dates like the first Monday of February, I tried strtotime("first monday february $year") and I've discovered that this does not work for 2010, since 02/01/2010 is a Monday - I get February 8th instead.
This bug is actually mentioned in the change log:
In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a month where that weekday was the first day of the month would incorrectly add one week to the returned timestamp. This has been corrected in 5.2.7 and later versions.
But I'm using PHP 5.3.8.
Why am I experiencing this error?
Looks like you are just missing an "of":
echo date('Y-m-d', strtotime('first monday of february 2010'));
will give the expected result. See the PHP Manual on Relative dates for the various input formats.
Depending on your version of PHP the 'of' statement may or may not work. As another solution try:
echo date('Y-m-d',strtotime('monday February 2010'));
will return the first monday of February 2010. Works for all days as well.

Strtotime function not working for Martin Luther King Day [duplicate]

echo date('Y-m-d', strtotime('First Monday '.date('F o', strtotime("-4 months")));
The above code returns 2012-10-08, which is incorrect - the first monday of october 2012 should return 2012-10-01.
The changelog from the manual (http://php.net/manual/en/function.strtotime.php) specifies the following:
In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a
month where that weekday was the first day of the month would incorrectly add
one week to the returned timestamp.
This has been corrected in 5.2.7 and later versions.
It seems to be clear that this is what's causing the wrong return date, however I'm using PHP version 5.4.7 (running xamp on localhost)!
Any ideas?
You are missing the keyword of when trying to retrieve the first day of the month
echo date('Y-m-d', strtotime('First Monday of '.date('F o', strtotime("-4 months")));
I found the info on this bug report
My current version is PHP 5.4.4 in case it still does not work for you

strtotime('First Monday ..') when it's the first day of the month returns the wrong date

echo date('Y-m-d', strtotime('First Monday '.date('F o', strtotime("-4 months")));
The above code returns 2012-10-08, which is incorrect - the first monday of october 2012 should return 2012-10-01.
The changelog from the manual (http://php.net/manual/en/function.strtotime.php) specifies the following:
In PHP 5 prior to 5.2.7, requesting a given occurrence of a given weekday in a
month where that weekday was the first day of the month would incorrectly add
one week to the returned timestamp.
This has been corrected in 5.2.7 and later versions.
It seems to be clear that this is what's causing the wrong return date, however I'm using PHP version 5.4.7 (running xamp on localhost)!
Any ideas?
You are missing the keyword of when trying to retrieve the first day of the month
echo date('Y-m-d', strtotime('First Monday of '.date('F o', strtotime("-4 months")));
I found the info on this bug report
My current version is PHP 5.4.4 in case it still does not work for you

Why is strtotime one week off?

strtotime("third Saturday October 2011")
Should be 10/15/2011. However, it's coming up at 10/22/2011. I assume this is because October 2011 starts on a Saturday and PHP is looking at the first full week. Since 10/1/2011 is a Saturday not a full week it ignores it.
Some research suggested putting "of" between the day of the week and the month should fix it but that doesn't work.
Any suggestions on why this is happening and what I can do to correct it?
This is a documented flaw in PHP <5.2.7 (see strtotime):
In PHP 5 prior to 5.2.7, requesting a given occurrence of a given
weekday in a month where that weekday was the first day of the month
would incorrectly add one week to the returned timestamp. This has
been corrected in 5.2.7 and later versions.
You'll need to upgrade PHP, or use a work around like Jonathan Kuhn suggests.
I was getting the same results as you on php 5.2.6. This works for me although not ideal.
echo date('Y-m-d', strtotime('Saturday October 2011 +2 weeks'));
<?php echo date('Y-m-d', strtotime('third Saturday of October 2011')); ?>
Works fine for me. Output is 2011-10-15.

work out the date of the fourth saturday in the current month

Bit stuck about how to go about this one. Given the current month, I need to to return the date of the fourth saturday of each month.
e.g. This month would be Feb 20th, next would be March 27th.
Thanks
I'm not a PHP coder, however, it seems strtotime is what you're after.
You can use strtotime("fourth Saturday") and it will return the 4th saturday.
Check out the strtotime docs.
EDIT:
Just to make the answer complete, thanks to Tom and Paul Dixon
date('dS F',strtotime('Fourth Saturday '.date('F o')));
You can use strtotime to find "next saturday" based on a starting date. If that starting date is the day before the earliest possible preceding day (21st) we get the answer...
//required year/month
$yyyymm="2009-01";
//find next saturday after earliest possible date
$t=strtotime("next saturday", strtotime("{$yyyymm}-21"));
//here you go!
echo "4th saturday of $yyyymm is ".strftime("%Y-%m-%d",$t)."\n";
Earliest possible 4th repeat of a day in any month is the 22nd (1,8,15,22), last possible 4th repeat is 28th (7,14,21,28).
EDIT: Although it's not clear in the documentation, you can request the "fourth saturday" too - use the zeroth day of the month as the basis:
$t=strtotime("fourth saturday", strtotime("{$yyyymm}-00"));
or omit the basis time and specify the month and year directly:
$t=strtotime("fourth saturday feb 2009");
Tip of the hat to Robin "I'm not a PHP coder" Day for spotting that :)
The earliest date for the fourth Saturday is the 22nd of the month. So look at the 22nd, see what day of the week it is, if it's not Saturday, add one day to the date, and check again, until you find a match (maximum you would have to check is 6 days).
Find the first Saturday of the month, and then add three weeks to that.
If you don't know when the first Saturday is (or, rather, don't know specifically a date corresponding with a day name), you might want to look at the Doomsday algorithm, which I conveniently looked at for another post with a somewhat similar issue.
function fourth_saturday($year, $month)
{
$info = localtime(mktime(0, 0, 0, $month , 1, $year));
return 28 - $info[6];
}
in PHP rather than pseudo code (think requires 5.2)
$date = getdate();
$date-> setDate($date->format('Y'), $date->format('Y'), '1'); // 1st of month.
while ($date->format('w' != 6)
$date->modify("+1 day");
$date->modify("+21 day"); // date is now on the fourth saturday

Categories