php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #78603 DateTime::createFromFormat() missing component behaviour change
Submitted: 2019-09-27 14:04 UTC Modified: 2022-06-02 16:11 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:1 (100.0%)
From: chris dot smith at widerplan dot com Assigned: derick (profile)
Status: Closed Package: Date/time related
PHP Version: 7.3.10 OS: Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: chris dot smith at widerplan dot com
New email:
PHP Version: OS:

Further comment on this bug is unnecessary.

 

 [2019-09-27 14:04 UTC] chris dot smith at widerplan dot com
Description:
------------
I'm not sure if this is a problem with the documentation or code.

The documentation states:

> If format does not contain the character ! then portions of the generated time which are not specified in format will be set to the current system time.

When executing the following based on the documentation I'd expect the hour, minute and seconds to be set to the current time however they are set to the epoch.

DateTime::createFromFormat('u', '123456');
// PHP < 7.3: 2019-09-27 14:50:40.123456
// PHP >= 7.3: 2019-09-27 00:00:00.123456

Further investigation led me to discover that the following example also contradicts the documentation and has been the case since the days of PHP 5.3.

DateTimeImmutable::createFromFormat('s', '12');
// 2019-09-27 00:00:12.000000
// Expected (per docs): 2019-09-27 14:50:12.000000

https://3v4l.org/v2bib

I ran a git bisect to identify when the microsecond handling changed which found: 09340ea9b9b2a06c6f1369502806640779073885

Test script:
---------------
<?php

$dt1 = DateTime::createFromFormat('u', '123456');
$dt2 = DateTime::createFromFormat('s', '25');

var_dump($dt1->format('H:i'));
var_dump($dt2->format('H:i:s'));

Expected result:
----------------
string(5) "14:29"
string(8) "14:29:25"

Actual result:
--------------
string(5) "00:00"
string(8) "00:00:25"

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-09-27 14:05 UTC] requinix@php.net
-Type: Bug +Type: Documentation Problem
 [2019-09-27 14:05 UTC] requinix@php.net
If it's been this way since PHP 5.3 then the problem is the documentation.
 [2019-09-27 14:11 UTC] chris dot smith at widerplan dot com
You are probably correct about it being documentation but there could also be some tests added to ensure the behaviour does not change in future.
 [2019-09-30 08:48 UTC] insidion at gmail dot com
This is absolutely a bug.

Testing it against anything pre 7.3 and it works as the documentation shows.
 [2019-09-30 11:02 UTC] chris dot smith at widerplan dot com
> Testing it against anything pre 7.3 and it works as the documentation shows.

If you do DateTime::createFromFormat('s', '12'); then the other parts of the time component are set to zero, i.e. the epoch this has been the same since 5.3 which does not follow the documentation. As of 7.3 `u` now works the same as seconds/minutes/hours, question is which behaviour is correct.

My thinking is that it should do what the documentation says but it's been broken so long and nobody has noticed it, that it makes more sense to change the documentation to match reality.
 [2022-02-14 04:02 UTC] requinix@php.net
-Block user comment: No +Block user comment: Yes
 [2022-06-02 16:11 UTC] derick@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: derick
 [2022-06-02 16:11 UTC] derick@php.net
Thank you for your bug report. This issue has already been fixed
in the latest released version of PHP, which you can download at
http://www.php.net/downloads.php

I fixed this wrt another Doc bug earlier today.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 23:01:28 2024 UTC