Is WordPress's timezone supposed to be set to UTC within themes and plugins?

爱⌒轻易说出口 提交于 2019-12-20 04:19:00

问题


I'm building a custom theme and plugin for WordPress and noticed that calls to date_default_timezone_get() return "UTC" even though:

  1. PHP is configured to the local timezone.
  2. 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)?


回答1:


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.



来源:https://stackoverflow.com/questions/40010946/is-wordpresss-timezone-supposed-to-be-set-to-utc-within-themes-and-plugins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!