PHP date format /Date(1365004652303-0500)/

后端 未结 10 1139
隐瞒了意图╮
隐瞒了意图╮ 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.

提交回复
热议问题