如何回显JSON字符串并同时替换该字符串的一部分?

时间:2015-09-04 14:04:28

标签: php json

我正在解码JSON响应并将其输出到一个简单的表中。以下是代码示例:

<?php

foreach($results['Events'] as $values)

{
        echo '<tr><td>' . $values['Title'] . '</td>';
        echo '<td>' . $values['Details']['Venue'] . '</td>';
        echo '<td>Event date:' . $values['Details']['Date'] . '</td></tr>';
}

?>

由于Date添加了时区,因此我得到的是:

Event title | Event venue | Event date
Event one | Venue one | 2016-01-01T00:00:00
Event two | Venue two | 2016-01-02T00:00:00
Event three | Venue three | 2016-01-03T00:00:00

有什么方法可以在回显结果时删除“T00:00:00”?这是一个理想的结果:

Event title | Event venue | Event date
Event one | Venue one | 2016-01-01
Event two | Venue two | 2016-01-02
Event three | Venue three | 2016-01-03

2 个答案:

答案 0 :(得分:5)

使用DateTime:

$date = new \DateTime($values['Details']['Date']);
echo '<td>Event date:' . $date->format('Y-m-d') . '</td></tr>';

答案 1 :(得分:3)

你也可以使用strtotime()

echo '<td>Event date:' . date("Y-m-d",strtotime($values['Details']['Date'])) . '</td></tr>';