php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #71489 date_parse_from_format does not work with unix timestamp
Submitted: 2016-02-01 14:24 UTC Modified: 2016-02-01 20:13 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: igorsantos07 at gmail dot com Assigned:
Status: Open Package: Date/time related
PHP Version: 5.6.17 OS: Linux Mint 16
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2016-02-01 14:24 UTC] igorsantos07 at gmail dot com
Description:
------------
Using date_parse_from_format() with a Unix Timestamp yields no error, but does not yields valid results as well. It sets the date to 1970-1-1, instead of the correct date.

Test script:
---------------
$ php -a
Interactive mode enabled

php > $unix = date('U');
php > echo $unix;
1454336317
php > echo date('Y-m-d H:i:s', $unix);
2016-02-01 12:18:37
php > print_r(date_parse_from_format('U', $unix));
Array
(
    [year] => 1970
    [month] => 1
    [day] => 1
    [hour] => 0
    [minute] => 0
    [second] => 0
    [fraction] => 0
    [warning_count] => 0
    [warnings] => Array
        (
        )

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

    [is_localtime] => 1
    [zone_type] => 1
    [zone] => 0
    [is_dst] => 
    [relative] => Array
        (
            [year] => 0
            [month] => 0
            [day] => 0
            [hour] => 0
            [minute] => 0
            [second] => 1454336317
        )

)

Expected result:
----------------
It should fill in the array with "2016-02-01 12:18:37" info.

Actual result:
--------------
The array is filled with "1970-01-01", without error messages.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-02-01 20:13 UTC] requinix@php.net
-Type: Bug +Type: Feature/Change Request
 [2016-02-01 20:13 UTC] requinix@php.net
https://3v4l.org/5aeV3

It's always seemed to me that date_parse_from_format() is more about extracting values from a date string, not deriving a complete date from a given string. But I don't think there would be a problem with adding an exception where the entire date is populated if the input string has a timestamp. At the very least, year=1970 and such is a bit odd.
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Thu Nov 26 04:01:23 2020 UTC