在这个小时内获得秒数的最有效方法


Most efficient way to get seconds left in this hour

我需要获取实际小时内的剩余秒数来缓存天气数据。最有效的方法是什么?

只需使用 PHP 的时间函数,如下所示:

<?php
function getSecondsRemaining()
{
    //Take the current time in seconds since the epoch, modulus by 3600 to get seconds in the hour, then subtract this from 3600 (# secs in an hour)
    return 3600 - time() % (60*60);
}
?>

这应该能让你得到你想要的性能。

你去伙计:

$currentMinute = date("i");
$minuteLeft = 60 - $currentMinute;
$secondLeft = $minuteLeft * 60;
$secondLeft += date("s");
echo $secondLeft;

怎么样

$date = new DateTime("now");
$seconds_left = ( ( 59 - $date->format("i") ) * 60 ) + ( 60 - $date->format("s") );