date/timezone problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

date/timezone problem

Post by speedy33417 »

I have a problem with dates and timezones. I use this code to get the current date and time:

Code: Select all

$date = date("l, Y F j, H:i:s T O I");
echo $date;
I'm in FL, which is EST (or UTC -6).

If I run my code at 10:12am I get this output:
Tuesday, 2006 October 17 07:12:01 MST -0700 0

Questions:
Why am I getting MST time?

How do I get EST time regardless where the user is?

My goal would be to save all times used by my database in EST, then depending on user's selected timezone (saved in db) recalculate it for displaying.

Basically my main concern is getting the time in EST, the rest I'll probably be able to do.
Maybe a hint/tutorial about changing stored time to different timezone.

Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

The server says it's in MST. EST is UTC-4, by the way.

Code: Select all

[feyd@home]>php -r "echo gmdate('l, Y F j, H:i:s \E\S\T -0400 I', time() - (4 * 60 * 60));"
Tuesday, 2006 October 17, 10:41:15 EST -0400 0
It's better to save all the times in GMT, just so you know.

edit: for thoroughness, here's how to get GMT:

Code: Select all

[feyd@home]>php -r "$str = 'l, Y F j, H:i:s'; $t = time(); echo gmdate($str, $t) . PHP_EOL . date($str, $t - date('Z'));"
Tuesday, 2006 October 17, 14:52:34
Tuesday, 2006 October 17, 14:52:34
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

Thanks feyd.

First of all I was wrong. EST is GMT - 0500.
Right now it's 11:20AM in FL (EST) and 16:20 in England. I know this for a fact, because my brother lives there.
Using your code.

Code: Select all

$str = 'l, Y F j, H:i:s';
$t = time();
echo gmdate($str, $t);
This code displays:
Tuesday, 2006 October 17, 15:20:51
So, it's one hour late. Is this a daytime saving time issue?

After reading your reply I settled on storing times in GMT, so it's important that I'll get the time right.

Also, running your second code:

Code: Select all

date($str, $t - date('Z'));
echo $date;
This code displayed:
Tuesday, 2006 October 17, 08:20:51 MST -0700 0

Again, that won't do, because that's the time in California and not in FL :)
Soooo, since my server is set wrong, I assume others' can be as well, which is no big deal. This is exactly why I have their preferred timezone saved in my db.

So I need a true GMT (not UTC) time -> $time_gmt.
The user's preferred timezone is stored in $user_timezone and has the value of EST, GMT, CET
I mostly deal with these three. CET = GMT +0100 and EST = GMT +0500

Before displaying any time I need a function that takes in $time_gmt and $user_timezone and gives me $time that is now calculated to the preferred time zone.

Any help would be great!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you're not running PHP 5, you'll have to artificially change the timezone, you can't use PHP's output of it because it will be the server's time -- Mountain time. (By the way, California is Pacific time)

You can use the output of date('I') to modify the time such that it is corrected.
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

Yes, I know that California is Pacific time, that's why I don't get why it's one hour off.
You're saying EST is -0400 GMT, but it's not. It's -0500.

Here's a link that proves me right :)
http://www.timeanddate.com/worldclock/

So what do I do to get that freaking GMT time? :D
It drives me nuts now.... :roll:

Code: Select all

echo gmdate('l, Y F j, H:i:s \G\M\T', time());
It should give me the current GMT time, but it's one hour off.
It gives me 16:28 when it's 17:28 in London...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Currently, the US is in Daylight Savings. That puts us back one hour, thus EST or EDT is GMT-4 (right now). Even the page you cite states that Miami is 4 hours behind UTC/GMT/Zulu.

http://webexhibits.org/daylightsaving/b.html
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

Thanks feyd for you time. I hate to be a pain in the you-know-what.

A quote from the webiste we talked about:

Current UTC (or GMT/Zulu)-time used: Tuesday, October 17, 2006 at 16:36:58
UTC is Coordinated Universal Time, GMT is Greenwich Mean Time.
Great Britain is one hour ahead of UTC during summer.

If you look at the time for Miami it says 12:36 and London 17:36 (that's 5 hours)

Basically what happens is I'm getting UTC and during summer I'll be 1 hour off. So, what I'm thinking is if I check the month and day and adjust accordingly that would fix my problem, right?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Adjust according to the daylight saving result from date('I'). Since your server says it's not in savings, that would place it in Arizona somewhere -- probably sitting next to Burrito.

Saving in UTC (which is what you're technically getting) is also perfectly acceptable. As long as it remains consistent is all that really matters, which it will.
Post Reply