tags:

views:

164

answers:

7

I'm looking to get the current date and time in a UNIX timestamp so I do some calculations but I am not getting the correct time. The date is correct but the time is off. I've already set my timezone so I'm lost on this. Can someone lend a hand?

Thanks.


Here is some of the code I am using:

date_default_timezone_set('America/Los_Angeles');
echo time();

Outputs: 1256926663 which is equal to Fri, 30 Oct 2009 18:17:43 GMT.

This is incorrect. What it should be is: Fri, 30 Oct 2009 10:17:43 PST


OK, the issue is not with the timestamp as I thought. I am using MySQL to interpret the timestamp but it is not correct. Here is what I am using:

FROM_UNIXTIME(timestamp, '%M %D, %Y - %l:%i %p') AS timestamp
+5  A: 

Sounds like your server has a wrong time set.
PHP doesn't have it's own clock.


// edit
it looks like the time it outputs is correct.
time() function outputs:

number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

This means GMT time.
What you're after is date(), which on the other hands outputs local time/date.

Michal M
Hi Mike. Thanks but the server time is set to central time and is correct.
Jim
A: 

If you're absolutely sure that your server has the correct time, and that you have set the timezone correctly, then by power of deduction I can only assume that the calculations you are using to parse the time are incorrect.

More detail would be helpful here, but it sounds to me that you've still got the timezone set incorrectly.

Thanks Anon. Is there a way to test the current timezone? Like a print_r or something?
Jim
Use date_default_timezone_get().
GZipp
GZipp summed it up quite nicely.Hope my suggestion helped.
+2  A: 

time() returns a unix timestamp which is ALWAYS gmt and ignores any locale settings. Use date() to format a unix stamp and respect locale settings.

Mike B
Hi Mike. Thanks. Do I need mktime() for this to get it into a UNIX timestamp?
Jim
No Jim. Unix timestamps are _always_ supposed to be UTC. You can't have a PST unix timestmap.
jason
use strtotime() to create a unix timestamp from almost any date format
Mike B
Ok, thanks Jason. Without having to add yet another column in my db table for a regular human readable date/time, what would you recommend to me to do with this? I need to show the user the date and time of a post and I had planned to do this with the unix stamp. Is this the best practice?
Jim
@Jim use date_default_timezone_set() to set the default timezone and date() to format the output to include the timezone indicator so the user knows.
Mike B
Thanks Mike but I no longer have a human readable date, only this unix timestamp. What should I do? Should I add another column to the database for a human readable dat/time or can I use this unix timestamp?
Jim
I'm already using date_default_timezone_set() and is set to los angeles.
Jim
another column is a waste of resource. if you store GMT time and then in your php get timezone offset: `date("Z")` which returns a number of seconds which you basically add to your timestamp stored in DB. and then output time.
Michal M
I've mentioned twice that date() formats a unix timestamp.
Mike B
Ok, thanks Mike. I agree, I didn't want to have to add another column when I already have the time in a unix timestamp. What is really strange is that there are like 100 ways to manipulate unix times and I have no idea why. It really should be more straightforward. I mean, there is strtotime, date(), mktime, etc...
Jim
It is straightforward. time() gives you seconds since epoch. The number of seconds since epoch doesn't change depending on what timezone you're in. The timezone affects how you interpret the number of seconds. Having one, consistent way to refer to a given point of time is one of the benefits of the way unix treats time.
jamessan
Thanks Jamessan, that helps actually. I have pinpointed my issue here. The issue is NOT with the timestamp but in how MySQL is calculating it. Should I post the SQL here or start a new thread?
Jim
Just update your question.
Michal M
OK, will do. Thanks for all the help guys.
Jim
@Jim Good luck :) - dates are anything but straightforward unfortunately.
Mike B
lol... I agree. Very convoluted. Who would have thought that a simple date could have been interpreted a hundred different ways. :)
Jim
Convoluted is right, but I'd say PHP and other languages do pretty well, considering: 40 official timezones, several unofficial timezones (one in Australia is UTC +8:45), daylight saving time, fractional offsets for timezones and daylight time, differing and changing daylight time starts and ends, different secular and religious calendars, leap years and leap seconds, the myriad ways dates and times are expressed, etc. And then there's Indiana.
GZipp
+1  A: 

Looks correct to me:

date_default_timezone_set('UTC');
echo date('r', 1256926663);
// Fri, 30 Oct 2009 18:17:43 +0000

date_default_timezone_set('America/Los_Angeles');
echo date('r', 1256926663);
// Fri, 30 Oct 2009 11:17:43 -0700
GZipp
Now this is interesting. brb. I'm going to try this in a new file and run it.
Jim
OK, your right GZipp. I think my problem is in the mysql date format because the unix timestamp is correct as you have showed but it renders wrong. I'm going to post my sql above.
Jim
strtotime($date) where $date is a MySQL date or datetime will return the corresponding unix timestamp in relation to PHP's current timezone
GZipp
GZipp. I'm first getting the unix timestamp through PHP, storing it in the database and then pull it out of the database and format it into what I need using mysql. So will strtotime will work for me then? I'm going to try it anyhow. :)
Jim
No, it won't work. You implied in your previous comment that you were storing in the db "a MySQL date or datetime", which is a string, and now say you're storing a unix timestamp. But strtotime() converts a date string to an integer (unix timestamp), not vice-versa.
GZipp
Right. It was my mistake. I'm actually storing the unix timestamp. I just want to get it back into the correct format so I can show it to the user. What do you suggest?
Jim
Where $timestamp is the timestamp retrieved from the database: echo date('Y-m-d H:i:s', $timestamp); (Use whatever format you wish in place of 'Y-m-d H:i:s'. See the PHP manual page for date()).
GZipp
A: 

http://codepad.org/BesoexCf

Returns the correct time (off by a few minutes for me, but that is expected).

What do you get when you run the above on your server?

Nick Presta
2009-10-30 11:10:06AM America/Los_Angeles is what I get.
Jim
And you know what? That is not correct because the minutes are off. On my local computer the mins are 54 not 06. :/
Jim
A: 

Consider checking your NTP daemon.

$ /etc/init.d/ntpd stop
$ ntpdate -q pool.ntp.org

# If it's off by much, jump it to the right time:

$ ntpdate pool.ntp.org
$ /etc/init.d/ntpd start

Make sure it starts up:

chkconfig ntpd --list

Good luck!

memnoch_proxy
+2  A: 

According to MySQL's docs, there are a couple things you need to pay attention to.

  1. The CURRENT_TIMESTAMP(), CURRENT_TIME(), CURRENT_DATE(), and FROM_UNIXTIME() functions return values in the connection's current time zone, which is available as the value of the time_zone system variable.

    So, the results of your query are already in the "local timezone", where what local is depends on various settings (both per-connection and global).

  2. In addition, UNIX_TIMESTAMP() assumes that its argument is a datetime value in the current time zone.

    The implication of this is that when you insert the timestamp into the database, you should either be calling it without an argument or ensuring that the date time string you give it is from the same timezone that MySQL is considering its local timezone for that connection.

Since it looks like you simply want to retrieve the information from the database and then decide how to format it in PHP, it may be wiser to simply pull out the unmodified timestamp from the database. Then your PHP code can handle making sure it is formatted according to the timezone that's relevant to the user.

jamessan
This is why I do not trust MySQL's date fields. They mess with my data.
jason