gpt4 book ai didi

PHP日期格式/Date(1365004652303-0500)/

转载 作者:IT王子 更新时间:2023-10-28 23:52:17 24 4
gpt4 key购买 nike

我正在调用一个 API,从中获取日期 /Date(1365004652303-0500)/,我不明白这是什么格式。这种日期格式怎么称呼?我不知道用什么谷歌搜索这种格式。

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

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

最佳答案

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

/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)/,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16749778/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com