Php 以格林威治标准时间格式转换时间


Php Convert time in gmt format

2012-06-27 16:17:06有这个字符串,我想将其转换为GMT格式.
我该怎么做?

多谢。

使用 gmdate().
使用 strtotime 将当前日期格式转换为 UNIX 时间戳,然后使用 gmdate($format, $timestamp);

好吧,严格来说你不能这样做。如果您不知道该日期是在哪个 TZ 中生成的,则无法将其转换为另一个 TZ。

如果该日期来自数据库,则可能您可以使用原始 TZ 查询日期。

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

PHP 的 DateTime 对象是一个不错的选择:

$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'); 
// 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;

试试这个,它会起作用

$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');

试试这个。

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

取自 如何在 PHP 中获取格林威治标准时间?