Alex Rivera | Logout

How do I get Greenwich Mean Time in PHP?

Asked 2009-05-12T07:30:40.543
46

I have a server which is set to EST, and all records in the database are set to EST. I would like to know how to set it to GMT. I want to offer a time zone option to my users.

Edit
Report

1 Answer

49

No matter in which GMT time zone the server is, here is an extremely easy way to get time and date for any time zone. This is done with the time() and gmdate() functions. The gmdate() function normally gives us GMT time, but by doing a trick with the time() function we can get GMT+N or GMT-N, meaning we can get the time for any GMT time zone.

For example, if you have to get the time for GMT+5, you can do it as follows

<?php 
  $offset=5*60*60; //converting 5 hours to seconds.
  $dateFormat="d-m-Y H:i";
  $timeNdate=gmdate($dateFormat, time()+$offset);
?>

Now if you have to get the time for GMT-5, you can just subtract the offset from the time() instead of adding to it, like in the following example where we are getting the time for GMT-4

<?php 
  $offset=4*60*60; //converting 4 hours to seconds.
  $dateFormat="d-m-Y H:i"; //set the date format
  $timeNdate=gmdate($dateFormat, time()-$offset); //get GMT date - 4
?>
answered 2009-05-12T07:39:17.753

Your Answer