简体   繁体   中英

Convert datetime and time zone to datetime

How to convert datetime with timezone to datetime. I have tried multiple which are available on PHP blog. But nothing worked out. I am retriving datetime from MySql query as 2017-01-20T13:59:19+03:00 format. But it shows the time zone separately as +03:00 . Actually the correct time is 2017-01-20 16:59:19 . So how can I show the correct time with adding GMT time in the datetime stamp.

I am facing this issue in Magento 2.

This is an opinionated answer but here goes: Convert the source datetime to a unix timestamp, convert the timezone to + or - a number of seconds (in your case 03:00 would be 3 hours worth of positive second change, or +10800). Arithmetic the two data points. Now you will have a corrected timestamp. Convert back to a datetime object and voila, completed.

Not the most pragmatic but probably one of the simplest to understand options.

Well i don't know this is a bad idea or what but can we do this?? i have tried this code on magento 1 on list.phtml file

$datetime = new DateTime('2017-01-20T13:59:19+03:00', new DateTimeZone('Asia/Kolkata'));
$new =  $datetime->format('Y-m-d H:i:s e');
$arr = (explode(" ",$new));
$arr2 = explode(":",$arr[1]);
$arr3 = explode(":",$arr[2]);
$vicky = $arr2[0]+$arr3[0];
$arr2[0] = $vicky;
$finaltime = (implode(":",$arr2));
$final =  $arr[0].' '.$finaltime;
echo $final;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM