【发布时间】:2016-02-22 06:22:58
【问题描述】:
我们如何转换2016-03-01T03:00:00Z这种日期格式
2016-03-01T03:00:00Z
转入01/02/2016 03:00 AM
同时维护时区信息。
一种方法是将带有“T”字符的字符串分解为“AM”和“PM”规范。
【问题讨论】:
-
如何将 3 月 1 日变成 2 月 1 日?不用爆
T连strtotime都能看懂那个格式
我们如何转换2016-03-01T03:00:00Z这种日期格式
2016-03-01T03:00:00Z
转入01/02/2016 03:00 AM
同时维护时区信息。
一种方法是将带有“T”字符的字符串分解为“AM”和“PM”规范。
【问题讨论】:
T 连strtotime 都能看懂那个格式
你需要date 和strtotime:
<?php
$a = "2016-03-01T03:00:00Z";
echo date("d/m/Y H:i A",strtotime($a));
?>
【讨论】:
我认为我们必须考虑时区 ('maintaining the timezone information')。如果默认时区与时间字符串的时区不同,结果将不正确。就像这种情况:
<?php
date_default_timezone_set('Asia/Shanghai');
$a = '2016-03-01T03:00:00Z';
echo date("d/m/Y H:i A",strtotime($a));
?>
结果是01/03/2016 11:00 AM
时间格式是ISO 8601,它将包含时区。
这是我的代码:
<?php
$a = '2016-03-01T03:00:00Z';
$tmpTime = explode('T',$a);
$resutTime = implode('/',array_reverse(explode('-',$tmpTime[0]))).' '.substr($tmpTime[1],0,5).' '.(intval(substr($tmpTime[1],0,2)) < 12 ? 'AM' : 'PM');
echo $resutTime;
?>
【讨论】: