Php Convert time in gmt format

余生颓废 提交于 2019-12-23 18:35:33

问题


I have this string 2012-06-27 16:17:06 and I want to convert it to GMT format.
How can I do that?

Thanks a lot.


回答1:


PHP's DateTime Object is a good choice:

$GMT = new DateTimeZone("GMT");
$date = new DateTime( "2011-01-01 15:00:00", $GMT );
$date->setTimezone( $newTZ );
echo $date->format('Y-m-d H:i:s'); 



回答2:


Use gmdate().
Convert your current date format to UNIX timestamp by using strtotime and then use gmdate($format, $timestamp);




回答3:


Well, strictly speaking you can not do it. If you do not know in what TZ that date has been generated you can not convert it to another TZ.

If that date came from a DB, probably you can query a date with the original TZ.

$date = new DateTime( "2011-01-01 15:00:00", $original_TZ ); 
$date->setTimezone( "GMT" );



回答4:


// Set timeline
$time_line = time() +7200; // <-- this is timeline +2
$h = gmdate('h', $time_line);
$i = gmdate('i', $time_line);
$s = gmdate('s', $time_line);

$time = $h.":".$i.":".$s;

echo $time;



回答5:


Try this, It will work

$newTZ = new DateTimeZone("Asia/Calcutta");
date_default_timezone_set("Asia/Calcutta");  
$GMT = new DateTimeZone("GMT");
$date = new DateTime( "2018-09-20 6:00:00 PM", $newTZ );
$date->setTimezone($GMT);
echo $date->format('Y-m-d H:i:s');



回答6:


Try this.

$time = '2012-06-27 16:17:06';
echo date("l, F j, Y, g:i a",strtotime($time) ); // assuming gmt format

taken from How do I get Greenwich Mean Time in PHP?



来源:https://stackoverflow.com/questions/11319653/php-convert-time-in-gmt-format

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!