How to get the timestamp range of this hour?

和自甴很熟 提交于 2019-12-24 07:39:05

问题


How to get the UNIX timestamp range of the current hour, i mean one of the first second and the other for the last second. so if it's 18:45 i would get the timestamp of 18:00 and 18:59.

Thanks


回答1:


You can get the components of the current time with getdate(), and use mktime() to find the timestamps:

$date = getdate();
$start = mktime($date['hours'], 0, 0);
$end = $start + (60*60);

You can also use date(), which is slightly simpler:

$start = mktime(date("H"), 0, 0);
$end = $start + (60*60);



回答2:


$start = mktime(date('H'), 0, 0);
$end = mktime(date('H'), 59, 59);

Could be generalized for any timestamp, not just the current time, as:

$time = time(); // some timestamp
$start = mktime(date('H', $time), 0, 0, date('n', $time), date('j', $time), date('Y', $time));
$end = mktime(date('H', $time), 59, 59, date('n', $time), date('j', $time), date('Y', $time));


来源:https://stackoverflow.com/questions/2679828/how-to-get-the-timestamp-range-of-this-hour

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!