我的理解是 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/