php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #47846 Error in DateTime class when initialising with '2009-02'
Submitted: 2009-03-31 08:44 UTC Modified: 2009-04-08 01:00 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:0 (0.0%)
From: brooksie155 at yahoo dot com Assigned:
Status: No Feedback Package: Date/time related
PHP Version: 5.2CVS-2009-03-31 (snap) OS: CentOS
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2009-03-31 08:44 UTC] brooksie155 at yahoo dot com
Description:
------------
There is a bug in the constructor of this object if you are only specifying the month and the year when dealing with the month of February.





Reproduce code:
---------------
$date = new DateTime('2009-02');
echo $date->format('Y-m');

Expected result:
----------------
2009-02

Actual result:
--------------
2009-03

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-03-31 08:56 UTC] rasmus@php.net
Not a bug.  As documented, this defaults to now and you can override individual components in the constructor.  In your case you have chosen Feb. 2009, but didn't specify a day of the month.  PHP picks todays day, which is the 31st day of the month.  The 31st day of Feb will fall in March since Feb doesn't have 31 days.
 [2009-03-31 08:56 UTC] derick@php.net
I can not reproduce this, what does the following output:

<?php
$str = "2009-02";
echo date_default_timezone_get(), "\n";
var_dump( date_parse( $str ) );
$date = new DateTime( $str );
echo $date->format( DateTime::ISO8601 ), "\n";
?>
 [2009-03-31 09:01 UTC] derick@php.net
Just as a comment to Rasmus, that's not true. The format "2009-02" implicitly sets the day nr as 1.
 [2009-03-31 09:11 UTC] rasmus@php.net
The output from your script on my box:

America/Los_Angeles
array(12) {
  ["year"]=>
  int(2009)
  ["month"]=>
  int(2)
  ["day"]=>
  bool(false)
  ["hour"]=>
  bool(false)
  ["minute"]=>
  bool(false)
  ["second"]=>
  bool(false)
  ["fraction"]=>
  bool(false)
  ["warning_count"]=>
  int(0)
  ["warnings"]=>
  array(0) {
  }
  ["error_count"]=>
  int(0)
  ["errors"]=>
  array(0) {
  }
  ["is_localtime"]=>
  bool(false)
}
2009-03-03T00:00:00-0800

That 'false' there on the date will make it default to today's date.  Seeing it on both 5.2 and 5.3 on Freebsd.  
 [2009-04-08 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Dec 07 05:03:41 2021 UTC