PHP日期格式/Date(1365004652303-0500)/ [英] PHP date format /Date(1365004652303-0500)/

查看:25
本文介绍了PHP日期格式/Date(1365004652303-0500)/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从获取日期的位置调用 API /Date(1365004652303-0500)/,我不明白这是什么格式.这种日期格式如何命名?我不确定要在 google 上搜索什么类型的格式.

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 sure what to google for such type of format.

谁能帮我以 Y-m-d H:i:s 格式获取这个日期?

Can anyone help me out in getting this date in Y-m-d H:i:s format?

我正在调用的 API 位于 .NET 服务器上.当我使用 PHP 的 file_get_contentsjson_decode 调用它时,它为我提供了以下创建日期的日期格式:/Date(1365004652303-0500)/

The API I am calling is on .NET server. And when I call it using PHP's file_get_contents and json_decode it gives me the following Date format for created date: /Date(1365004652303-0500)/

推荐答案

首先你需要了解你拥有的格式

First you need to understand the format you have

/Date(1365004652303-0500)/

然后你有

  • 时间戳 (U) = 1365004652
  • 毫秒 (u) = 303
  • 与格林威治时间 (GMT) 的差异 (O) = -0500

建立格式

$date = '/Date(1365004652303-0500)/';
preg_match('/(d{10})(d{3})([+-]d{4})/', $date, $matches);
$dt = DateTime::createFromFormat("U.u.O",vsprintf('%2$s.%3$s.%4$s', $matches));
echo $dt->format('r');

输出

Wed, 03 Apr 2013 15:57:32 -0500
                            ^
                            |= Can you see the GMT ? 

<小时>

interface DateFormatParser
{
    /**
     * @param $string
     *
     * @return DateTime
     */
    public function parse($string);

}

abstract class PregDateParser implements DateFormatParser
{
    protected $pattern, $format, $mask;

    public function parse($string) {
        $string = (string)$string;

        $pattern = $this->pattern;
        $format  = $this->format;
        $mask    = $this->mask;

        $r = preg_match($pattern, $string, $matches);
        if (!$r) {
            throw new UnexpectedValueException('Preg Regex Pattern failed.');
        }
        $buffer = vsprintf($mask, $matches);
        $result = DateTime::createFromFormat($format, $buffer);
        if (!$result) {
            throw new UnexpectedValueException(sprintf('Failed To Create from Format "%s" for "%s".', $format, $buffer));
        }
        return $result;
    }
}

class JsonTimestampWithOffsetParser extends PregDateParser
{
    protected $pattern = '/^/Date((d{10})(d{3})([+-]d{4}))/$/';
    protected $format  = 'U.u.O';
    protected $mask    = '%2$s.%3$s.%4$s';
}

$date   = '/Date(1365004652303-0500)/';
$parser = new JsonTimestampWithOffsetParser;
$dt     = $parser->parse($date);

echo $dt->format('r');

这篇关于PHP日期格式/Date(1365004652303-0500)/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆