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: 2021-09-23 15:47 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: cmb (profile)
Status: Closed Package: Date/time related
PHP Version: 5.6.17 OS: Linux Mint 16
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: igorsantos07 at gmail dot com
New email:
PHP Version: OS:

 

 [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.
 [2021-09-23 15:47 UTC] cmb@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cmb
 [2021-09-23 15:47 UTC] cmb@php.net
> https://3v4l.org/5aeV3

This is implemented as of PHP 8.0.0.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 23 17:01:31 2024 UTC