PHP一种计算午夜前时间的更简单方法


PHP A simpler way to calculate time until midnight?

有比这更简单的方法来计算一天中剩余的时间吗?

$midnight = clone $endDate;
$midnight->setTime(24,0);
$dayLeft = $endDate->diff($midnight);

我希望有一个使用DateTime和"午夜"的单行解决方案。使用明天午夜不起作用,因为相对于设置为DateTime的$endDate,我需要午夜。非常感谢。

也许这对你有帮助:

$secsTillMidnight = strtotime('next day 0:0')-time();

其中

  • time()给出自1970年1月1日以来的当前时间(也称为"Unix时间")

  • strtotime()将字符串转换为Unix时间。