Unix Time 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
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Unix Time Problem

Post by spacebiscuit »

Hi,

I'm having some trouble with formatting a Unix time stamp. I am retreiving a timestamp from a MySQL database, for example:

1292318785 - Tue, 14 Dec 2010 09:26:25

I am formatting this using:

Code: Select all

$mytime=date('H:i:s', $unixtime);
However this outputs:

3:26:25 - which is 6 hours behind. It seems there is an offset?

Any advice would be apprecaited.

Thanks,

Rob.
User avatar
Darhazer
DevNet Resident
Posts: 1011
Joined: Thu May 14, 2009 3:00 pm
Location: HellCity, Bulgaria

Re: Unix Time Problem

Post by Darhazer »

the output depends on the server timezone / timezone set in php.ini
You can use gmdate() however to get timezone-independent output
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Unix Time Problem

Post by klevis miho »

Use the date_default_timezone_set function in the beginning of the script.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Unix Time Problem

Post by spacebiscuit »

Thanks that fixed the problem - i'm not quite sure why it is necessary though. The time has already been stamped and stored, I could understand if I was grabbing the curent live time and I wanted this to be correct for each user but that is not the case here.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Unix Time Problem

Post by klevis miho »

Yes but the timestamp "1292318785" is relative to the server.
spacebiscuit
Forum Contributor
Posts: 390
Joined: Mon Mar 07, 2005 3:20 pm

Re: Unix Time Problem

Post by spacebiscuit »

I thought a unix timestamp was relative to Unix rather than the server?

Rob.
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: Unix Time Problem

Post by klevis miho »

It's the number of seconds from January 1970 till now, but the "now" depends on the server.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Unix Time Problem

Post by pickle »

The timestamp is in GMT. So a timestamp of 0 on a server in Chicago would be January 1, 1970 minus 7 hours.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply