Jim
Jim

Reputation: 1

php time is not giving the correct date

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

Upvotes: 0

Views: 1617

Answers (7)

jamessan
jamessan

Reputation: 42757

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.

Upvotes: 2

memnoch_proxy
memnoch_proxy

Reputation: 1954

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!

Upvotes: -1

Nick Presta
Nick Presta

Reputation: 28705

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?

Upvotes: 0

GZipp
GZipp

Reputation: 5426

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

Upvotes: 1

Mike B
Mike B

Reputation: 32155

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.

Upvotes: 2

Michal M
Michal M

Reputation: 9480

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.

Upvotes: 5

Anonymous
Anonymous

Reputation:

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.

Upvotes: 0

Related Questions