Php 最有效的方法是在这一小时内获得剩余的秒数

Php 最有效的方法是在这一小时内获得剩余的秒数,php,datetime,Php,Datetime,我需要获取实际小时中的剩余秒数来缓存天气数据。最有效的方法是什么?给你,伙计: $currentMinute = date("i"); $minuteLeft = 60 - $currentMinute; $secondLeft = $minuteLeft * 60; $secondLeft += date("s"); echo $secondLeft; 给你,伙计: $currentMinute = date("i"); $minuteLeft = 60 - $currentMinute;

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

给你,伙计:

$currentMinute = date("i");
$minuteLeft = 60 - $currentMinute;
$secondLeft = $minuteLeft * 60;
$secondLeft += date("s");
echo $secondLeft;
给你,伙计:

$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") ); 
怎么样

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

只需使用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);
}
?>


这将获得您想要的性能。

只需使用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);
}
?>


这应该可以让您获得所需的性能。

我也想到了这一点,但这真的是最有效的方法吗?我是个表演迷,所以…:)我也想到了——但这真的是最有效的方法吗?我是个表演迷,所以…:)没问题!很高兴我能帮助你我喜欢这一款,它很优雅。很好的解决方案Flyingeagle!没问题!很高兴我能帮助你我喜欢这一款,它很优雅。很好的解决方案Flyingeagle!