php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51096 strtotime returns wrong results
Submitted: 2010-02-20 15:18 UTC Modified: 2014-07-31 13:45 UTC
Votes:9
Avg. Score:3.7 ± 0.8
Reproduced:7 of 7 (100.0%)
Same Version:1 (14.3%)
Same OS:3 (42.9%)
From: phpbugs at rizzt dot kicks-ass dot org Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 5.2.12 OS: linux
Private report: No CVE-ID: None
 [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



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-02-20 15:20 UTC] phpbugs at rizzt dot kicks-ass dot org
Sorry, I forgot to change 2 & 3 

Expected result:
----------------
1 2010/02/20
2 2010/02/01
3 2010/02/28
4 2010/03/20
5 2010/03/01
6 2010/03/31
 [2010-02-20 15:22 UTC] derick@php.net
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/31

 [2010-02-20 17:10 UTC] derick@php.net
Actually, there is a bug here "first day" and "last day" should be "+1 day" and "-1 day".
 [2010-03-06 17:04 UTC] derick@php.net
Automatic comment from SVN on behalf of derick
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=295892
Log: - Fixed bug #51096 ('last day' and 'first day' are handled incorrectly when
  parsing date strings).
- For 5.2 I just added the test case minus new 5.3 additions.
 [2010-03-06 17:05 UTC] derick@php.net
-Status: Assigned +Status: Closed
 [2010-03-06 17:05 UTC] derick@php.net
This bug has been fixed in SVN.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 [2010-03-09 09:49 UTC] derick@php.net
-Status: Closed +Status: Re-Opened
 [2010-03-09 09:49 UTC] derick@php.net
Re-opening because of the question marks:

firstdayof = 'first day of'?;
lastdayof = 'last day of'?;

It shouldn't cause an issue though, but it needs fixing.
 [2010-03-10 22:12 UTC] jani@php.net
-Status: Re-Opened +Status: Assigned
 [2010-03-31 14:23 UTC] marques at displague dot com
This 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#97065
 [2010-03-31 14:33 UTC] marques at displague dot com
I'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 -0500
 [2010-03-31 15:14 UTC] marques at displague dot com
This one is a clearer case of strtotime being bad:

echo date("Y-m-d",strtotime("february", strtotime("march 31 2010")));
2010-03-03
 [2010-03-31 15:32 UTC] derick@php.net
@marques: You're wrong, that's perfectly correct behaviour for last month/previous month. It is also unrelated to this report.
 [2010-03-31 21:12 UTC] wayne530 at gmail dot com
@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?
 [2014-07-31 13:45 UTC] salathe@php.net
-Status: Assigned +Status: Closed
 [2014-07-31 13:45 UTC] salathe@php.net
Regarding "Re-opening because of the question marks ... It shouldn't cause an issue though, but it needs fixing.", that was fixed today.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 04 07:01:26 2024 UTC