3

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.

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
michele
  • 26,348
  • 30
  • 111
  • 168
  • possible duplicate of [Convert date to gmt - php](http://stackoverflow.com/questions/2796015/convert-date-to-gmt-php) – Oliver Jul 04 '12 at 10:36

6 Answers6

6

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

Nikola K.
  • 7,093
  • 13
  • 31
  • 39
  • $time=strtotime("2012-06-27 16:17:06"); gmdate($format, $time); but what value i have to assign at $format? – michele Jul 03 '12 at 21:58
2

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" );
Ivan Buttinoni
  • 4,110
  • 1
  • 24
  • 44
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'); 
j0k
  • 22,600
  • 28
  • 79
  • 90
Sam Ellis
  • 781
  • 2
  • 6
  • 15
1
// 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;
Jason Sturges
  • 15,855
  • 14
  • 59
  • 80
Andrew
  • 25
  • 4
1

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

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?

Community
  • 1
  • 1
jarchuleta
  • 1,231
  • 8
  • 10