Reputation: 2238
How we convert2016-03-01T03:00:00Z this date format like
2016-03-01T03:00:00Z
into 01/02/2016 03:00 AM
also maintaining the timezone information.
One method is to explode the string with "T" character but problem into the 'AM' and 'PM' specification.
Upvotes: 0
Views: 5903
Reputation: 1
I think we must consider the timezone ('maintaining the timezone information'
). If default timezone different from the time string's timezone, the result will not correct.Just like this case:
<?php
date_default_timezone_set('Asia/Shanghai');
$a = '2016-03-01T03:00:00Z';
echo date("d/m/Y H:i A",strtotime($a));
?>
the result is 01/03/2016 11:00 AM
The time formate is ISO 8601
,it will contain the timezone.
This is my code:
<?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;
?>
Upvotes: 0
Reputation: 3515
You need date
with strtotime
:
<?php
$a = "2016-03-01T03:00:00Z";
echo date("d/m/Y H:i A",strtotime($a));
?>
Demo : http://codepad.org/O73INg7p
Upvotes: 2