PHP date format /Date(1365004652303-0500)/

后端 未结 10 1065
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 05:56

I am calling an API from where I am getting date /Date(1365004652303-0500)/, I don\'t understand what format this is. How is this date format called? I was not

相关标签:
10条回答
  • 2020-11-27 06:33

    Baba's answer is very helpful, yet it does not cover 3 cases I need:

    • The timestamp may be negative
    • The timestamp may be less than 10 digits
    • GMT offset may be missing.

    This is what I use for getting the timestamp:

    $date_string = '/Date(-594262800300+0100)/';
    preg_match('/([+-]?\d+)([+-]\d{4})?/', $date_string, $matches);
    $timestamp = $matches[1] / 1000;
    $hours = $matches[2] * 36; // Get the seconds
    return $timestamp + $hours;
    

    Again, as Ghigo noted this is not correct if the offset contains minutes, but it works for my case.

    0 讨论(0)
  • 2020-11-27 06:34

    You can use this package to parse the JSON dates:

    https://github.com/webapix/dot-net-json-date-formatter

    use \Webapix\DotNetJsonDate\Date;
    
    $dateTime = Date::toDateTime('/Date(1365004652303-0500)/'); 
    // return with \DateTime object, you can format it: $dateTime->format('Y-m-d H:i:s')
    
    0 讨论(0)
  • 2020-11-27 06:39

    If your date is like /Date(-62135578800000)/, a positive or negative integer without timezone:

    $date = substr('/Date(-62135578800000)/', 6, -5);
    $date = date('m/d/Y H:i:s', $date + date('Z', $date) * -1);
    // 01/01/0001 05:00:00
    
    0 讨论(0)
  • 2020-11-27 06:42

    Let's break /Date(1365004652303-0500)/ down to:

    • Date
    • 1365004652303
    • -0500

    First string makes itself pretty clear.

    The next large number is the epoch value

    The -0500 represents the timezone in which the dates were originally stored. It is relative to UTC and thus, it is referring to Eastern Standard Time.


    EDIT

    The epoch is with a milisecond precision. Try this code:

    <?php
        $str = "/Date(1365004652303-0500)/";
        preg_match( "#/Date\((\d{10})\d{3}(.*?)\)/#", $str, $match );
        echo date( "r", $match[1] );
    ?>
    

    You can also use the timezone for setting the date relative to your own. http://codepad.viper-7.com/RrSkMy

    0 讨论(0)
提交回复
热议问题