我使用strtotime("first day of this month", time());
来获取当前月份的开始,strtotime("midnight", time());
来获取当前日期的开始。现在我想知道当前小时的开始时间。
strtotime("last hour", time());
给出当前小时,减1。
看文档,我看到你可以构建表达式来获得你想要的时间,然而,我已经尝试了几个,我卡住了。"这一小时的第一秒"或"这一小时的第一秒"answers"这一小时"(与"现在"相同)都给我错误的值。
如何使用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();
输出:Tuesday, 13-Oct-15 02:00:00 UTC
1444701600
您也可以使用strtotime(date('Y-m-d H:00:00'))
据我所知,没有办法使用口头描述获得一小时开始的时间戳。即使"last hour"
也只返回now() - 3600
。
你最好的选择是:
$iCurrentTimestamp = strtotime("now");
$iStartOfHour = $iCurrentTimestamp - ($iCurrentTimestamp % 3600);
现在模数3600将返回小时开始后的秒数,然后从当前时间减去。
获取一天的开始时间
$starttime = date("Y-m-d H:i:s",strtotime("midnight"));
到当前时间
$endtime = date("Y-m-d H:i:s");