Edit report at https://bugs.php.net/bug.php?id=55240&edit=1

 ID:                 55240
 Updated by:         der...@php.net
 Reported by:        m dot strucken at binserv dot de
 Summary:            DateTime::createFromFormat with format 'd.m.Y Gi'
                     returns wrong date
 Status:             Assigned
-Type:               Bug
+Type:               Feature/Change Request
 Package:            Date/time related
 Operating System:   Windows Server 2008 R2
 PHP Version:        5.3.6
 Assigned To:        derick
 Block user comment: N
 Private report:     N

 New Comment:

Hi!

I've had a look at this, and now I can tell you what happens.
Without the delimiter, the 800 gets parsed as 80 hours, 0 minutes.
If you add 80 hours to  11.11.2009, that means 3 days, 8 hours, you get 
14.11.2009, 8am.

If you call DateTime::getLastError, you see it shows a warning:

derick@whisky:~$ cat /tmp/test.php 
<?php
$dt = DateTime::createFromFormat( 'd.m.Y Gi', '11.11.2009 800' );
var_dump($dt);
var_dump(DateTime::getLastErrors());
echo $dt->format( 'd.m.Y G i' );
?>

derick@whisky:~$ php /tmp/test.php 
class DateTime#1 (3) {
  public $date =>  string(19) "2009-11-14 08:00:00"
  public $timezone_type =>  int(3)
  public $timezone =>  string(13) "Europe/London"
}
array(4) {
  'warning_count' =>
  int(1)
  'warnings' =>
  array(1) {
    [14] =>
    string(27) "The parsed time was invalid"
  }
  'error_count' =>
  int(0)
  'errors' =>
  array(0) {
  }
}
14.11.2009 8 00
derick@whisky:~$ 

The parser for "G" parses up to 2 characters, and it doesn't know how to stop 
if it encounters something too high. I'm looking at how I can changes this, but 
still not break BC in any form. I've changed the bug's status to FR now.

cheers,
Derick


Previous Comments:
------------------------------------------------------------------------
[2011-07-19 11:11:45] m dot strucken at binserv dot de

Description:
------------
Having a date without a delimiter between hour and minute and the hour without 
leading zero, the returned date from DateTime::createFromFormat() is incorrect

Test script:
---------------
$dt = DateTime::createFromFormat( 'd.m.Y Gi', '11.11.2009 800' );
echo $dt->format( 'd.m.Y Gi' );

Expected result:
----------------
11.11.2009 800

Actual result:
--------------
14.11.2009 800


------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=55240&edit=1

Reply via email to