我以字符串格式获得了日期和时间的JSON响应,我希望它能转换为正确的日期和时间。 我使用API来获取响应并且使用JSON代码,但它是以字符串形式。如何将其转换为适当的日期和时间。
我收到了以下回复:
"start_time": 1492841592,
"end_time": 1492841619,
"seconds": 22392,
"duration": "00:00:27",
"_drm": 0.45,
我想要输出:
01:10pm 16 Mar
此格式。
要进行转换,我尝试使用 PHP 中的date()
函数。但它不会给我正确的结果。
我提到了以下问题:
How to convert string to date format in PHP?
答案 0 :(得分:0)
你在找这样的东西吗?
$json = <<<'JSON'
{
"start_time": 1492841592,
"end_time": 1492841619,
"seconds": 22392,
"duration": "00:00:27",
"_drm": 0.45
}
JSON;
$dt = json_decode($json);
echo date('h:ia M j', $dt->start_time);
输出:
06:13am Apr 22