PHP date format /Date(1365004652303-0500)/

后端 未结 10 1103
隐瞒了意图╮
隐瞒了意图╮ 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:20

    This is what I'm using to parse timestamps from a Xero API. It accounts for:

    • Negative timestamps, dates before the epoch.
    • Timestamps with less than 10 digits (but on the epoch - time zero - not sure what that format would even look like)
    • Fractions of a second.
    • Optional timezone offset, both positive and negative.

    Code:

    if (preg_match('#^(/Date\()([-]?[0-9]+)([0-9]{3})([+-][0-9]{4})?(\)/)$#', $data, $matches)) {
        // Handle Xero API DateTime formats. Examples:
        // "/Date(1436961673000)/" - unix timestamp with milliseconds
        // "/Date(1436961673000+0100)/" - with an additional timezone correction
        // "/Date(-1436961673000-0530)/" - before the epoch, 1924 here
        //
        // RE matches for "/Date(1436961673090+0100)/":
        // [1] = (/Date\()          "/Date("
        // [2] = ([-]?[0-9]+)       "1436961673"    epoch seconds
        // [3] = ([0-9]{3})         "090"           milliseconds
        // [4] = ([+-][0-9]{4})?    "+0100" or ""   optional timezone
        // [5] = (\)/)              ")"
    
        $result = \DateTime::createFromFormat('U u', $matches[2] . ' ' . $matches[3] . '000')
            ->setTimezone(new \DateTimeZone($matches[4] ?: '+0000'));
    }
    

提交回复
热议问题