Bug #74839 [Com]: DateTime::createFromFormat in PHP 7.0 creates wrong object
| From: | mattficken@php.net | Date: | Tue, 13 Mar 2018 01:25:22 +0000 |
| Subject: | Bug #74839 [Com]: DateTime::createFromFormat in PHP 7.0 creates wrong object | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-214315@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74839&edit=1
ID: 74839
Comment by: mattficken@php.net
Reported by: hksingla92 at gmail dot com
Summary: DateTime::createFromFormat in PHP 7.0 creates wrong
object
Status: Verified
Type: Bug
Package: Date/time related
Operating System: Windows 7
PHP Version: 7.0.20
Block user comment: N
Private report: N
New Comment:
I can repro on 7.2.2 on Windows (3v4l is PHP on Linux, I assume).
The failing section of the output:
[4] => DateTime Object
(
[date] => 2018-03-14 03:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
Should be 15:00 not 3:00.
A corrected repro script (for syntax only):
<?php
$test = array(
DateTime::createFromFormat('G:i', '3:00'),
DateTime::createFromFormat('G:i', '15:00'),
DateTime::createFromFormat('G:iA', '3:00AM'),
DateTime::createFromFormat('G:iA', '15:00AM'),
DateTime::createFromFormat('G:iA', '15:00PM')
);
print_r($test);
?>
Previous Comments:
------------------------------------------------------------------------
[2018-03-09 12:11:47] cmb@php.net
Confirmed: <https://3v4l.org/F9uj3>.
------------------------------------------------------------------------
[2017-07-01 06:02:15] hksingla92 at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/datetime.createfromformat
---
While creating a datetime object using following function
DateTime::createFromFormat('G:iA', '15:00PM');
produced this object
DateTime Object
(
[date] => 2017-07-02 03:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
Its mixing "G/H" and "A" to produce wrong results as it adds extra hours
resulting in wrong date and time. May be we should ignore "A" in case if "G/H"
is used instead of "g/h"
Test script:
---------------
I think this array has all possible scenarios:
$test = [
DateTime::createFromFormat('G:i', '3:00');
DateTime::createFromFormat('G:i', '15:00');
DateTime::createFromFormat('G:iA', '3:00AM');
DateTime::createFromFormat('G:iA', '15:00AM');
DateTime::createFromFormat('G:iA', '15:00PM');
];
print_r($test);
Expected result:
----------------
I was expecting to see object at last index with correct time like following
DateTime Object
(
[date] => 2017-07-01 15:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
Actual result:
--------------
It produced following object:
DateTime Object
(
[date] => 2017-07-02 03:00:00.000000
[timezone_type] => 3
[timezone] => UTC
)
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74839&edit=1