php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #71728 date functions leading zeros ingnore
Submitted: 2016-03-07 10:08 UTC Modified: 2021-04-05 16:27 UTC
Votes:3
Avg. Score:3.7 ± 0.9
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: shadowspawnshadow at gmail dot com Assigned:
Status: Open Package: Documentation problem
PHP Version: 5.5.33 OS: Windows
Private report: No CVE-ID: None
 [2016-03-07 10:08 UTC] shadowspawnshadow at gmail dot com
Description:
------------
Date functions/methods like date_parse_from_format(), date_create_from_format()/DateTime::createFromFormat() ignore leading zeros in format and date string. So doe not make sense you use, for example, 'm' or 'n', both '01' and '1' passed without errors or warnings.
Looks relevant to all versions from 5.2 to 5.6.

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

// date with leading zeros
$d = '1990-01-02';
// format without leading zeros
$f = 'Y-n-j';
print_r(date_parse_from_format($f, $d));
print_r(date_create_from_format($f, $d));
print_r(DateTime::getLastErrors());
echo '--------------------------';
// date without leading zeros
$d = '1990-1-2';
// format with leading zeros
$f = 'Y-m-d';
print_r(date_parse_from_format($f, $d));
print_r(date_create_from_format($f, $d));
print_r(DateTime::getLastErrors());

?>

Expected result:
----------------
Expect some warnings at least.

Actual result:
--------------
Array
(
    [year] => 1990
    [month] => 1
    [day] => 2
    [hour] => 
    [minute] => 
    [second] => 
    [fraction] => 
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 0
    [errors] => Array
        (
        )

    [is_localtime] => 
)
DateTime Object
(
    [date] => 1990-01-02 05:00:38.000000
    [timezone_type] => 3
    [timezone] => America/New_York
)
Array
(
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 0
    [errors] => Array
        (
        )

)
--------------------------
Array
(
    [year] => 1990
    [month] => 1
    [day] => 2
    [hour] => 
    [minute] => 
    [second] => 
    [fraction] => 
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 0
    [errors] => Array
        (
        )

    [is_localtime] => 
)
DateTime Object
(
    [date] => 1990-01-02 05:00:38.000000
    [timezone_type] => 3
    [timezone] => America/New_York
)
Array
(
    [warning_count] => 0
    [warnings] => Array
        (
        )

    [error_count] => 0
    [errors] => Array
        (
        )

)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-03-07 10:13 UTC] requinix@php.net
-Status: Open +Status: Feedback
 [2016-03-07 10:13 UTC] requinix@php.net
Is this creating an actual problem for you? Or how about we just change the documentation to say that leading zeroes are ignored?
 [2016-03-07 10:34 UTC] shadowspawnshadow at gmail dot com
-Status: Feedback +Status: Open
 [2016-03-07 10:34 UTC] shadowspawnshadow at gmail dot com
Actualy I can work around this with regexp, but it is awkward. An example: Laravel framework use date_parse_from_format() for date format validation, what looks usefull actualy (no need to have format and regexp for same property). Yes you can change documentation, but I think it would be untidy. Also will be duplicated format characters for parsing and different for formating.
 [2021-04-05 16:27 UTC] derick@php.net
-Package: Date/time related +Package: Documentation problem
 [2021-04-05 16:27 UTC] derick@php.net
This is expected behaviour, but should be noted in the documentation.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 15:01:30 2024 UTC