php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #49480 DateTime::createFromFormat uses current system time to fill missing fields
Submitted: 2009-09-06 06:48 UTC Modified: 2009-09-12 09:09 UTC
From: grodny at oneclick dot sk Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5.3.0 OS:
Private report: No CVE-ID: None
 [2009-09-06 06:48 UTC] grodny at oneclick dot sk
Description:
------------
DateTime::createFromFormat uses current system time to fill missing fields instead of zero values, which leads to inconsistent comparsions
and intervals returned by DateTime::diff.

DateTime::createFromFormat('Y-m-d', '2009-03-02') should always create

object(DateTime)#1 (3) {
  ["date"]=>
  string(19) "2009-03-02 00:00:00"
  ...
}


Reproduce code:
---------------
<?php

$A = DateTime::createFromFormat('Y-m-d', '2009-03-02');
sleep(2);
$B = DateTime::createFromFormat('Y-m-d', '2009-03-02');

var_dump($A == $B);

?>

Expected result:
----------------
bool(true)

Actual result:
--------------
bool(false)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-09-06 10:44 UTC] derick@php.net
This is correct behaviour, you can change it by using a format like:

<?php
var_dump( DateTime::createFromFormat('!Y-m-d', '2009-03-02') );
?>
object(DateTime)#1 (3) {
  ["date"]=>
  string(19) "2009-03-02 00:00:00"
  ["timezone_type"]=>
  int(3)
  ["timezone"]=>
  string(13) "Europe/London"
}

 [2009-09-08 07:34 UTC] grodny at oneclick dot sk
Such inconsistency and non-determinism in simple DateTime instance creation without using format modifier(undocumented one) can't be called correct. It's just how it was done :)
Thanks anyway for hint.
 [2009-09-08 09:09 UTC] derick@php.net
Let's keep it open though, so that it can be documented.
 [2009-09-12 09:09 UTC] svn@php.net
Automatic comment from SVN on behalf of torben
Revision: http://svn.php.net/viewvc/?view=revision&revision=288278
Log: Document how to cause unspecified portions of the time given to
createFromFormat() to be initialized to zero.
Addresses bug #49480.
 [2009-09-12 09:09 UTC] torben@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Dec 04 22:00:01 2025 UTC