我正在从获取日期的地方调用API /Date(1365004652303-0500)/,但我不知道这是什么格式。该日期格式如何称呼?我不知道用什么格式的谷歌。
/Date(1365004652303-0500)/
谁能帮我得到这个日期的Y-m-d H:i:s格式?
Y-m-d H:i:s
我正在调用的API在.NET服务器上。当我使用PHP调用它时file_get_contents,json_decode它为创建的日期提供了以下日期格式:/Date(1365004652303-0500)/
file_get_contents
json_decode
首先,您需要了解自己的格式
那你有
建立格式
$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');