Search code examples
phpwordpressdatetimezonegmt

PHP - output of date('now') is a ahead of local time by 6 hours, but time zone is set to correct local zone zone?


I'm in St. Louis, MO in the USA Central Time Zone. We are -6 GMT

The PHP time zone setting in php.ini is date.timezone = "America/Chicago"

(Chicago is almost directly north of St. Louis.

echo date('now'); produces: December 19, 2015, 2:45 am. EDIT - I meant date("F j, Y, g:i a", strtotime("now"));, not date('now');

Locally (by calendar and clock) it is December 18, 2015, 8:45 pm.

Here is the date information as output by phpinfo()

date/time support   enabled
"Olson" Timezone Database Version   2015.1
Timezone Database   internal
Default timezone    America/Chicago

Directive               Local Value     Master Value
date.default_latitude   31.7667         31.7667
date.default_longitude  35.2333         35.2333
date.sunrise_zenith     90.583333       90.583333
date.sunset_zenith      90.583333       90.583333
date.timezone           America/Chicago America/Chicago

I thought that if the correct time zone is set, date('now') would return the local time and time - but I'm obliviously wrong.

What am I missing about the date() function or PHP date handling in general?

SimonT


Solution

  • There are a few different ways you can choose to handle this:

    • You can use date_default_timezone_set to override Wordpress's time zone setting.

    • You can use the PHP DateTime class instead of the date function, which accepts a time zone in the constructor

    • You can change Wordpress's time zone setting in the WP Admin / Settings / General section:

    screenshot