Why is strtotime one week off? - php

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.

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

PHP strtotime wrong only on sundays

Currently I am using the following to get the date of monday and friday of this and next week.
$curWeekStart = date('d.m', strtotime('monday this week'));
$curWeekEnd = date('d.m', strtotime('friday this week'));
$nextWeekStart = date('d.m', strtotime('monday next week'));
$nextWeekEnd = date('d.m', strtotime('friday next week'));
All works fine until the day "sunday" comes, like today.. then this things above only show me monday and not other days. Tuesday, Wednesday, Thursday, Friday ecc are not working, it is only returning me monday. This error happens only on sundays and not on other days.
That's the output:
$curWeekStart == 13.03;
$curWeekEnd == 13.03;
$nextWeekStart == 20.03;
$nextWeekEnd == 20.03;
Does anyone have an idea how to solve this? Or do you know an alternative to this?
This is likely the result of a bug that affected PHP versions 5.6.23 to 5.6.30, 7.0.8 to 7.0.16, and 7.1.0 to 7.1.2. It also affects the DateTime class.
The bug was resolved in PHP 7.0.17 and 7.1.3, but is still present in the latest release of PHP 5.6 (5.6.30 at the time of writing), which now receives only security fixes and therefore probably won't ever receive a fix.
Upgrading PHP to an unaffected version is probably the best solution, but if you need a workaround for an affected version you should be able to add a DateInterval to a Datetime and get the result you want.

this week in PHP 5.3

I try to find an answer but without result
the problem is:
in sunday function return date for Monday next week
$date = new DateTime();
$date->setTimezone(new DateTimeZone('Europe/Moscow'));
$date->setTimestamp(strtotime('Monday this week'));
echo $date->format("d.m.Y");
but in other days (except Sunday) its return correct value of Monday.
I ever set locale manualy , which has a monday - the first day of week, but PHP "think" the Sunday is still firs day. is it bug ?? or i do some wrong ?
There seem to be known idiosyncrasies with strtotime. It's mentioned in the comments:
http://www.php.net/manual/en/datetime.formats.relative.php#108317
They don't mention different locales specifically but it would be extremely unsurprising if strtotime() does not correctly behave for next/this week under different locales.
Try this Monday or next Monday and see if one of those gets what you want.

PHP Strtotime erratic function

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.

Categories