php - php date() 和 time() 函数的可移植性

标签 php date time cron

我的理解是 php 原生的 date() 和 time() 函数会根据服务器位置的本地时间给我一个时间戳或日期格式。所以首先,如果我对此有误,请随时告诉我。

因此,如果是这种情况,我需要编写一些函数来确定 cron 作业的时间以查看它是否应该执行,我需要将其自定义为默认时区。如果我错了,请再次纠正我。

话虽这么说,如果我这样做然后更改托管服务器,我将需要重写确定时间正确的文件,因为 gmt 的时区偏移现在会不同。

是这样还是有更好的方法?

最佳答案

date() and time() functions native to php will give me a timestamp or date formatted based on the local time of the location of the server

并非 100% 正确。 time() 将返回相同的值,无论位置、时区、设置等如何,因为所有 unix 时间戳都是非 UTC。

其次,date() 将根据 PHP 的时区设置(通常与服务器相同)返回格式化值。参见 php.net/date_default_timezone_set有关示例和更多信息。强烈建议(甚至通过 PHP 自己的错误报告)您的应用程序尽早设置默认时区以避免出现问题。

So that being said, if I did that and then changed hosting servers, i would need to rewrite the file that determined the time correct because timezone offset of gmt would now be different.

这就是为什么在您的应用程序中设置 date_default_timezone_set 如此重要。这给了它更多的便携性,所以当你移动服务器时,你不需要担心改变它的时间设置。

关于php - php date() 和 time() 函数的可移植性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7983759/

相关文章:

php - PHP 单例类是否以某种方式在请求之间共享?

events - 5秒内按任意键

java - Joda-Time 的大致时间段

r - 从 R 中的天数得出日期?

sql - 查询以检查开始日期和结束日期之间的日期范围(如果已存在于 postgres 的数据库中)

sql - 如何处理postgresql日期字段中的空值

python - 从时间中减去小时和分钟

php - Bootstrap 表延伸到容器之外

php - php rand函数有问题

php glob - 在子文件夹中扫描文件