Php Convert time in gmt format

2019-07-04 04: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.

标签: php time gmt
6条回答
我命由我不由天
2楼-- · 2019-07-04 05:12
// 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;
查看更多
SAY GOODBYE
3楼-- · 2019-07-04 05:23

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'); 
查看更多
迷人小祖宗
4楼-- · 2019-07-04 05:29

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" );
查看更多
做自己的国王
5楼-- · 2019-07-04 05:30

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');
查看更多
Bombasti
6楼-- · 2019-07-04 05:31

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

查看更多
smile是对你的礼貌
7楼-- · 2019-07-04 05:31

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?

查看更多
登录 后发表回答