tags:

views:

4147

answers:

4

I have a server which is set to EST and all records in the db are set to EST. i would like to know how to set it to gmt? I want to put a time zone option to my users.

+5  A: 

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

For example you have to get time for GMT+5 we will do it like following

<?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 which is GMT-5 now we will just subtract the offset from the time() instead of adding into time like in following example we are getting time for GMT-4

<?php 
  $offset=4*60*60; //converting 5 hours to seconds.
  $dateFormat="d-m-Y H:i";
  $timeNdate=gmdate($dateFormat, time()-$offset);
?>
p01nd3xt3r
Thank you for your answer
drikoda
+2  A: 

Posted by General Cucombre

in Yahoo answers:

$today_gmt_is = date("l, F j, Y, g:i a", time() - date("Z")) ;


date("Z") gives you offset of your local time from GMT in seconds. subtract that value from local time to convert local time to GMT time.

* 1 year ago

also: php.net

Babiker
I will delete soon.
Babiker
this is a baaaad way to do it.
nickf
this is a really bad way to do this...
p01nd3xt3r
well i tried deleting it, i cannot!
Babiker
+4  A: 

I would strongly suggest avoiding messing with UNIX timestamps to make it look like a different time zone. This is a lesson I've learnt the hard way, way too many times.

A timestamp is the number of seconds since Midnight 1 January 1970, GMT. It doesn't matter where you are in the world, a given timestamp represents the exact same moment in time, regardless of time zones. Yes, the timestamp "0" means 10am 1/1/70 in Australia, Midnight 1/1/70 in London and 5pm 31/12/69 in LA, but this doesn't mean you can simply add or subtract values and guarantee accuracy.

The golden example which stuffed me up every year for way too long was when daylight savings would crop up. Daylight savings means that there are "clock times" which don't exist in certain parts of the world. For example, in most parts of the US, there was no such time as 2:01am on April 2, 2006.

Enough quasi-intelligible ranting though. My advice is to store your dates in the database as timestamps, and then...

For example,

$timestamp = time();

echo "BRISBANE: " . date('r', $timestamp) . "\n";
echo "     UTC: " . gmdate('r', $timestamp) . "\n";
date_default_timezone_set('Africa/Johannesburg');
echo "  JOBURG: " . date('r', $timestamp) . "\n";

// BRISBANE: Tue, 12 May 2009 18:28:20 +1000
//      UTC: Tue, 12 May 2009 08:28:20 +0000
//   JOBURG: Tue, 12 May 2009 10:28:20 +0200

This will keep your values clean (you don't have to be worrying about adding offsets, and then subtracting before saving), it will respect all Daylight Savings rules, and you also don't have to even look up the timezone of the place you want.

nickf
This is too screwy. And p01nd3xt3r's method is sound. If timezone X is, say, "7 hours ahead" of UTC, that is just another way of saying that the clocks in timezone X are now showing the same time that a UTC clock _will_ show 7 hours from now. So the question is "what is the current time in timezone X?" will always have the same answer as the question "what will the UTC time be in 7 hours?" -- at least until the timezone offset changes, whether due to Daylight Saving Time or whatever.
Robert L
"at least until the timezone offset changes, whether due to Daylight Saving Time or whatever." ... and you're calling my solution screwy?
nickf
Yes, because you need a PHP library capable of this, and also what if the politicians change the rules, and what if you have other code which gets the local date, ...
Robert L
"a PHP library capable of this" - it's built in. "what if the politicians change the rules" - good: you won't have to change any of your code - if PHP is kept updated, then it'll just work. "what if you have other code which gets the local date" - rinse and repeat.
nickf
A: 

i've a site hosted on media temple, i have to get uk daylight saving time but failed to get it either using gmdate or by setting date_default_timezone_set('GMT') . Need a solution.

Thanks