I'm building a custom theme and plugin for WordPress and noticed that calls to date_default_timezone_get()
return "UTC" even though:
- PHP is configured to the local timezone.
- Wordpress is set to the local timezone in Settings:General:Timezone.
Is this correct behaviour for WordPress, or do I have something misconfigured, a bad plugin, etc.?
Do I need to switch to my local timezone before any calls to PHP's date and time functions (and then restore it later)?
Yes, WordPress does work in UTC internally so the values you get from PHP’s date()
, time()
, etc. functions will be in UTC as well. You could wrap these in calls to date_default_timezone_set()
but that gets messy.
It's generally easier to use WordPress’s built-in current_time() function.
It can return a formatted date (like you would get using PHP’s date()
function) or a timestamp (like you would get by calling PHP’s time()
function). A returned time value can be used to seed PHP's other date/time functions. By default current_time()
returns values for the local timezone.
NOTE: Do not switch to your local timezone using date_default_timezone_set()
before calling current_time()
or you will get bad values from it.