|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2010-02-20 15:18 UTC] phpbugs at rizzt dot kicks-ass dot org
Description:
------------
strtotime with "first day next month" or "last day next month" does not
work
Reproduce code:
---------------
echo strftime('1 %Y/%m/%d')."\n";
echo strftime('2 %Y/%m/%d', strtotime('first day'))."\n";
echo strftime('3 %Y/%m/%d', strtotime('last day'))."\n";
echo strftime('4 %Y/%m/%d', strtotime('next month'))."\n";
echo strftime('5 %Y/%m/%d', strtotime('first day next month'))."\n";
echo strftime('6 %Y/%m/%d', strtotime('last day next month'))."\n";
Expected result:
----------------
1 2010/02/20
2 2010/02/21
3 2010/02/19
4 2010/03/20
5 2010/03/01
6 2010/03/31
Actual result:
--------------
1 2010/02/20
2 2010/02/21
3 2010/02/19
4 2010/03/20
5 2010/03/21
6 2010/03/19
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 22:00:01 2025 UTC |
That's because those things were introduced in PHP 5.3: derick@kossu:~$ pe 5.2dev derick@kossu:~$ php <?php echo strftime('1 %Y/%m/%d')."\n"; echo strftime('2 %Y/%m/%d', strtotime('first day'))."\n"; echo strftime('3 %Y/%m/%d', strtotime('last day'))."\n"; echo strftime('4 %Y/%m/%d', strtotime('next month'))."\n"; echo strftime('5 %Y/%m/%d', strtotime('first day next month'))."\n"; echo strftime('6 %Y/%m/%d', strtotime('last day next month'))."\n"; ?> 1 2010/02/20 2 2010/02/21 3 2010/02/19 4 2010/03/20 5 2010/03/21 6 2010/03/19 derick@kossu:~$ pe 5.3dev derick@kossu:~$ php <?php echo strftime('1 %Y/%m/%d')."\n"; echo strftime('2 %Y/%m/%d', strtotime('first day'))."\n"; echo strftime('3 %Y/%m/%d', strtotime('last day'))."\n"; echo strftime('4 %Y/%m/%d', strtotime('next month'))."\n"; echo strftime('5 %Y/%m/%d', strtotime('first day next month'))."\n"; echo strftime('6 %Y/%m/%d', strtotime('last day next month'))."\n"; ?> 1 2010/02/20 2 2010/02/01 3 2010/02/28 4 2010/03/20 5 2010/03/01 6 2010/03/31This may be fodder for another bug report, but strtotime() more specifically "returns wrong results" for the last three days of March 2010. date_default_timezone_set('America/New_York'); echo PHP_VERSION."\n". date(DATE_RFC822)."\n". date(DATE_RFC822,strtotime('last month')); 5.3.1 Wed, 31 Mar 10 08:21:30 -0400 Wed, 03 Mar 10 08:21:30 -0500 This was also posted as a PHP.net comment: http://www.php.net/manual/en/function.strtotime.php#97065I'm not sure there would be consensus on my expected return values here, but it makes more sense then getting March as a result. Reproduce code: --------------- date_default_timezone_set('America/New_York'); echo date(DATE_RFC822,strtotime('march 31 2010 -1 month')); echo date(DATE_RFC822,strtotime('march 30 2010 -1 month')); echo date(DATE_RFC822,strtotime('march 29 2010 -1 month')); echo date(DATE_RFC822,strtotime('march 28 2010 -1 month')); Expected result: ---------------- Sun, 28 Feb 10 00:00:00 -0500 Sun, 28 Feb 10 00:00:00 -0500 Sun, 28 Feb 10 00:00:00 -0500 Sun, 28 Feb 10 00:00:00 -0500 Actual result: ---------------- Wed, 03 Mar 10 00:00:00 -0500 Tue, 02 Mar 10 00:00:00 -0500 Mon, 01 Mar 10 00:00:00 -0500 Sun, 28 Feb 10 00:00:00 -0500This one is a clearer case of strtotime being bad: echo date("Y-m-d",strtotime("february", strtotime("march 31 2010"))); 2010-03-03@derick can you comment on what exactly should be the behavior of strtotime('-1 month') or strtotime('last month')? today 3/31/10 date('Y-m-d', strtotime('-1 month')) returns '2010-03-03' date('Y-m-d', strtotime('+1 month')) returns '2010-05-01' -1 month seems to offset by 28 days. +1 month seems to offset by 31 days. is this correct behavior?