php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #18670 strtotime() bug
Submitted: 2002-07-31 10:26 UTC Modified: 2003-10-18 22:57 UTC
Votes:8
Avg. Score:3.9 ± 0.9
Reproduced:7 of 7 (100.0%)
Same Version:3 (42.9%)
Same OS:0 (0.0%)
From: nic at bbmcarlson dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 4.3.3RC5-dev OS: Linux
Private report: No CVE-ID: None
 [2002-07-31 10:26 UTC] nic at bbmcarlson dot com
I have some code:

$nextmonth = strtotime("next month");
$monthafter = strtotime("next month",$nextmonth") ;

$monthOne = date("F") ;
$monthTwo=date("F",$nextmonth) ;
$monthThree=date("F",$monthafter) ;

Today is July 31 2002. I have the code:

print($monthOne) ; 
print($monthTwo) ;
print($monthThree) ;

It prints: July, August, October

I expect it to print: July, August, September

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-07-31 10:38 UTC] nohn@php.net
Strange... With PHP4.3.0-dev-200207300000 on Compaq Tru64 the output is "July October December".
 [2002-07-31 10:39 UTC] rasmus@php.net
The problem is that it adds a full month to the current timestamp.  So you end up getting the 31st of the next month and some months do not have 31 days.  I'll need to do some reading to determine if strtotime() is supposed to pick the beginning of the next month instead, or perhaps the middle.
 [2002-07-31 10:44 UTC] rasmus@php.net
Sebastian, yes, that is because I changed how "next" works based on bug report #18655

It works correctly for days now, but it does seem like month-handling is messed up now.  
 [2002-09-24 17:07 UTC] spud at nothingness dot org
In PHP 4.2.3, the difference between "2" and "next" are still screwy in strtotime(). I'm trying to parse icalendar recurrence formats, so I need to calculate things like the "second Monday" in a month. Sample code below illustrates the difference between "2" and "next" (which should be identical).

<?
$start = strtotime('September 1, 2002');
echo ('Start timestamp: '.$start.'<br>');
echo ('Start date: Sunday, Sep 1 2002<br>');
$first = strtotime('first Monday',$start);
echo ('"First" Monday: '.date('l, M d Y',$first).'<br>');
$oneth = strtotime('1 Monday',$start);
echo ('"1" Monday: '.date('l, M d Y',$oneth).'<br>');
$next = strtotime('next Monday',$start);
echo ('"Next" Monday: '.date('l, M d Y',$next).'<br>');
$twoth = strtotime('2 Monday',$start);
echo ('"2" Monday: '.date('l, M d Y',$twoth).'<br>');
$third = strtotime('third Monday',$start);
echo ('"Third" Monday: '.date('l, M d Y',$third).'<br>');
$threeth = strtotime('3 Monday',$start);
echo ('"3" Monday: '.date('l, M d Y',$threeth).'<br>');
?>
 [2002-10-31 12:07 UTC] matt at codewalkers dot com
I also can confirm that strtotime acts funny when the same day does not exist in the next month:

<?
$timestamp = strtotime("31 October 2002");

$next_month = strftime("%B" ,strtotime("+1 month", $timestamp));

echo $next_month;
?>

displays:

December
 [2003-02-11 14:12 UTC] mphillips at lufkin dot org
I have noticed that when you do something like
$sdate = date9'Y-m-d', strtotime('02-09-2003'));

$sdate is getting '2008-02-24'.

Is this a common occurance
 [2003-10-18 17:45 UTC] lsmith@php.net
well atleast on php 4.3.3 "2" and "next" seem to be working as expected in returning the same results using the script provided by spud at nothingness dot org.

the comment by mphillips at lufkin dot org is unrelated to the bug report and is actually not a bug anyways since the format is not supported (with '/' as separators it works as expected)

finally going by the manual this behaviour described by the bug report may be inconvinient but not really counter the documentation: "The unit of time displacement may be selected by the string `year' or `month' for moving by whole years or months. These are fuzzy units, as years and months are not all of equal duration. More precise units are `fortnight' which is worth 14 days, `week' worth 7 days, `day' worth 24 hours, `hour' worth 60 minutes, `minute' or `min' worth 60 seconds, and `second' or `sec' worth one second. An `s' suffix on these units is accepted and ignored."

i lack the confidence to mark this report as bogus so i am just marking it as feedback :-)
 [2003-10-18 18:04 UTC] andrey@php.net
Using the date submitted by nic_at_bbmcarcarlson_dot_com : 31 July 2002 :
The result both on my box and on the local dev server here is :
JulyOctoberDecember

Versions tested :
1)PHP_4_3 branch
2)HEAD branch

 [2003-10-18 22:57 UTC] sniper@php.net
This works as expected. (with PHP 4.3.3 and later)

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 03 06:01:30 2024 UTC