我strtotime("first day of this month", time());用来获取本月的开始,strtotime("midnight", time());的开始以获取当前日期的开始。现在我想获取当前小时的开始时间。
strtotime("last hour", time()); 给我当前小时,负 1。
查看文档,我发现您可以构建表达式来获得所需的时间,但是,我尝试了几次,但被卡住了。“这一小时的第一秒”或“这一小时的第一秒”和“这一小时”(与“现在”相同)都给了我不正确的值。
我将如何使用 获取当前小时的第一时刻的时间戳strtotime?
我会使用DateTime代替。您仍然可以使用与 strtotime 相同的格式,但它还为您提供了一个界面来执行 strtotime 无法执行的操作。
$date = new DateTime(); // Defaults to now.
$date->setTime($date->format('G'), 0); // Current hour, 0 minute, [0 second]
echo $date->format(DateTime::RFC850), "\n";
echo $date->getTimestamp();
Run Code Online (Sandbox Code Playgroud)
$date = new DateTime(); // Defaults to now.
$date->setTime($date->format('G'), 0); // Current hour, 0 minute, [0 second]
echo $date->format(DateTime::RFC850), "\n";
echo $date->getTimestamp();
Run Code Online (Sandbox Code Playgroud)
据我所知,无法使用口头描述来获取一小时开始的时间戳。甚至"last hour"只是回归now() - 3600。
您最好的选择如下:
$iCurrentTimestamp = strtotime("now");
$iStartOfHour = $iCurrentTimestamp - ($iCurrentTimestamp % 3600);
Run Code Online (Sandbox Code Playgroud)
现在模数 3600 将返回一小时开始后的秒数,然后从当前时间中减去。