Bug #71728 [Fbk->Opn]: date functions leading zeros ingnore

From: Date: Mon, 07 Mar 2016 10:34:07 +0000
Subject: Bug #71728 [Fbk->Opn]: date functions leading zeros ingnore
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-199642@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=71728&edit=1

 ID:                 71728
 User updated by:    shadowspawnshadow at gmail dot com
 Reported by:        shadowspawnshadow at gmail dot com
 Summary:            date functions leading zeros ingnore
-Status:             Feedback
+Status:             Open
 Type:               Bug
 Package:            Date/time related
 Operating System:   Windows
 PHP Version:        5.5.33
 Block user comment: N
 Private report:     N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2016-03-07 10:13:18] 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:08:13] 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
        (
        )

)


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



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


Thread (3 messages)

« previous php.bugs (#199642) next »