php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73269 DateTime::create_from_format treats d like j for day which is inconsistent
Submitted: 2016-10-08 11:33 UTC Modified: 2017-02-12 17:56 UTC
Votes:3
Avg. Score:4.0 ± 0.8
Reproduced:3 of 3 (100.0%)
Same Version:3 (100.0%)
Same OS:2 (66.7%)
From: contactme at antonydandrea dot com Assigned:
Status: Not a bug Package: Date/time related
PHP Version: 7.0.11 OS: Ubuntu 16.04.2
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: contactme at antonydandrea dot com
New email:
PHP Version: OS:

 

 [2016-10-08 11:33 UTC] contactme at antonydandrea dot com
Description:
------------
In the documentation, 'd' for dates is supposed to be a 2 digit character. So if it is less than 10, there is a 0 in front, e.g. 09.

DateTime::createfromformat is supposed to "Parses a time string according to a specified format" return false on failure. I would expect 'd' to reject strings that did not have two characters for strings.

Yet it seems to accept it as valid like 'j'. It looks like PHP tries to be clever and automatically treat is as 'j', but I think this behaviour is inconsistent.

Test script:
---------------
$dateObj = date_create_from_format('Y-m-d', '1930-12-3');
var_dump($dateObj->format('Y-m-d');

Expected result:
----------------
I expect that for the sake of consistency, PHP returns false and fails to create an object based on the format.

Actual result:
--------------
It returns the object and the var_dump has the string "1930-12-03".  

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-08 11:39 UTC] contactme at antonydandrea dot com
-Summary: DateTime::create from format doesn't have trailing 0. +Summary: DateTime::create_from_format treats d like j for day which is inconsistent
 [2016-10-08 11:39 UTC] contactme at antonydandrea dot com
Made title clearer
 [2017-02-12 17:56 UTC] derick@php.net
-Status: Open +Status: Not a bug
 [2017-02-12 17:56 UTC] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

This is by design, and documented on the createFromFormat page: http://php.net/manual/en/datetime.createfromformat.php#refsect1-datetime.createfromformat-parameters
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 07:00:02 2026 UTC