问题
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