【发布时间】:2011-01-20 17:31:30
【问题描述】:
我正在编写一个 PHP 系统,我需要获取系统时间。不是 GMT 时间或特定于时区的时间,而是 CRON 系统使用的相同系统时间。我有一个每天午夜运行的 CRON 作业,我想在网页上显示它需要多长时间才能再次运行。
例如: 现在我的系统时钟是下午 6 点。我运行代码:
$timeLeftUntilMidnight = date("H:i", strtotime("tomorrow") - strtotime("now"));
但是,结果是“3:00”而不是“6:00”。如果我跑
date("H:i", strtotime("tomorrow"));
它返回 0:00,这是正确的。但是如果我跑
date("H:i", strtotime("now"));
它返回 21:00,即使正确的应该是 18:00。
谢谢。
【问题讨论】:
-
让我试着澄清一下:如果我运行 date("H:i", time()),它会显示从现在起 3 小时后的时间。因此, strtotime("tomorrow") - strtotime("now") 给我的时间比正确时间少 3 小时。例如,现在是下午 6 点,cronjob 将在上午 12 点运行,但 strtotime("tomorrow") - strtotime("now") 返回 3 小时,因为“明天”是 00:00 而“现在”是晚上 9 点。
-
@JohnWithoutArms 你能用这个代码更新你的帖子吗?
-
使用
time()而不是strtotime("now") -
使用 time() 而不是 strtotime("now") 没有区别,仍然是相同的结果。
-
可以,但是time()不必解析字符串“now”并将其识别为当前时间