如何在WordPress中获取当前日期和时间? [英] How to get current date and time in WordPress?

查看:789
本文介绍了如何在WordPress中获取当前日期和时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您可能已经知道,WordPress强烈建议主题/插件作者不要通过 date_default_timezone_set

As you may know it's highly recommended by WordPress to theme/plugin authors to don't change the WordPress timezone by PHP functions like date_default_timezone_set.

此外,您在输入时可能会收到错误的日期和时间值使用php date 函数,因为未通过PHP设置默认时区。

I addition to that you may receive wrong date and time values when you use php date function because of not setting default timezone by PHP.

我的意思是您在 date('G')中可能会收到10,而在WordPress所选时区中确切的小时数是15 ->设置->常规->时区选项。

I mean you may receive 10 for date('G') while the exact hour is 15 in the selected timezone in WordPress->Settings->General->Timezone option.

如果您在WordPress网站的选定时区中需要正确的日期和时间值,该怎么办?

So what should you do if you need the correct date and time values in selected timezone for WordPress website?

推荐答案

您应使用 WordPress的current_time 功能而不是PHP日期函数来获取WordPress中的本地日期和时间。它将根据WordPress常规选项中的选定时区为您返回正确的值。

You should use current_time function of WordPress instead of PHP date function for getting local date and time in WordPress. It will return correct value for you based on selected timezone in WordPress general options.

对我来说 current_time('G')正好返回15,而PHP date('G')返回10,这是不正确的。

For me current_time('G') returns exactly 15 while PHP date('G') returns 10 which is not correct.

希望它因为我没有在stackoverflow中找到相关的线程,所以可以为某人提供帮助。

Hope it helps somebody since I didn't find related thread in the stackoverflow.

这篇关于如何在WordPress中获取当前日期和时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆