lang2
lang2

Reputation: 11966

php date offset

I'm using php's date() function to print out a time, which is retrieved from mysql. The actual time that's printed out is always one hour off.

I'm calling

date('H:i, d M Y', 100000)

I'm expecting "03:46, 02 Jan 1970" but got "04:46, 02 Jan 1970" instead.

Why?

Upvotes: 2

Views: 8096

Answers (2)

Wh1T3h4Ck5
Wh1T3h4Ck5

Reputation: 8509

Set timezone before, using date_default_timezone_set() because date() function depends of timezone setting (from php.ini or set by code)

That returns date/time according to your current timezone.

Check your actual timezone using

echo date_default_timezone_get();

Set (change) it using

date_default_timezone_set('UTC'); # UTC is just an example

For complete list of supported timezones in PHP check this link.

Example:

date_default_timezone_set('UTC');
echo date('H:i, d M Y', 100000);

Output:

03:46, 02 Jan 1970

Your current timezone offset is +0100 and that's why you've got 04:46, 02 Jan 1970. Also don't forget that back to 70's a lot of countries did not use DST rules.

References

Upvotes: 7

KushalP
KushalP

Reputation: 11196

Looks like you're having a timezone issue.

You can set the timezone for your PHP app using date_default_timezone_set('UTC');, replacing UTC to whatever you require. This should fix your problem.

For more info, see: http://php.net/manual/en/function.date.php

Upvotes: 2

Related Questions