WordPress 的时区是否应该在主题和插件中设置为 UTC? [英] Is WordPress's timezone supposed to be set to UTC within themes and plugins?
问题描述
我正在为 WordPress 构建自定义主题和插件,并注意到对 date_default_timezone_get()
的调用返回UTC",即使:
I'm building a custom theme and plugin for WordPress and noticed that calls to date_default_timezone_get()
return "UTC" even though:
- PHP 配置为本地时区.
- Wordpress 在 Settings:General:Timezone 中设置为本地时区.
这是 WordPress 的正确行为,还是我配置错误、插件错误等?
Is this correct behaviour for WordPress, or do I have something misconfigured, a bad plugin, etc.?
在调用 PHP 的日期和时间函数之前是否需要切换到本地时区(然后在稍后恢复)?
Do I need to switch to my local timezone before any calls to PHP's date and time functions (and then restore it later)?
推荐答案
是的,WordPress 在内部确实在 UTC 中工作,因此您从 PHP 的 date()
、time()<获得的值/code> 等功能也将采用 UTC.您可以将它们包装在对
date_default_timezone_set()
的调用中,但这会变得混乱.
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.
使用 WordPress 的内置 current_time() 函数通常更容易.
It's generally easier to use WordPress’s built-in current_time() function.
它可以返回一个格式化的日期(就像你使用 PHP 的 date()
函数一样)或一个时间戳(就像你通过调用 PHP 的 time()
函数得到的一样)).返回的时间值可用于为 PHP 的其他日期/时间函数设置种子.默认情况下,current_time()
返回本地时区的值.
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.
注意:在调用 current_time()
之前不要使用 date_default_timezone_set()
切换到本地时区,否则你会得到错误的值.
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.
这篇关于WordPress 的时区是否应该在主题和插件中设置为 UTC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!