开始和结束时间,分为1小时 [英] Start and end time, split into 1 hour segments
问题描述
我有一个timestamp
格式的开始时间和结束时间.我想将这些时间划分为1小时的时间段.
I have a start and end time in a timestamp
format. I want to split these into timeslots of e.g 1 hour.
$t1 = strtotime('2010-05-06 12:00:00');
$t2 = strtotime('2010-05-06 18:00:00');
$timeslots = array();
while ($t1 < $t2) {
$t1 = $t1 + 3600;
$timeslots[] = $t1;
}
foreach ( $timeslots as $slot ) {
echo date("Y-m-d H:i:s", $slot) . '<br/>';
}
这是最有效的方法吗?还是有更好,更通用的方法?
Is this the most efficient way to do it or is there a better, more versatile way to do this?
有时在不同长度的时隙上尝试使用其他数字时,会出现致命错误:允许的内存大小用尽,这使我觉得效率不是很高.尽管现在看来这似乎没有发生……
Occasionally when trying it with other numbers for different length timeslots there was a Fatal error: Allowed memory size exhausted which makes me think it's not very efficient. Though that doesn't appear to be happening now...
(我正在构建预订系统)
(I'm building a booking sytem)
推荐答案
您尝试过
while ($t1 < $t2) {
$t1 = strtotime('+1 hour', $t1);
$timeslots[] = date('Y-m-d H:i:s', $t1);
}
foreach ( $timeslots as $slot ) {
echo $slot . '<br/>';
}
有些相同,但更干净.如前所述,strtotime将处理date年等日期更改.您的PHP内存限制设置为多少?可能太低了.
Somewhat the same but cleaner. And as was said strtotime will handle date changes like leap years. What is your PHP memory limit set at? Might be too low.
这篇关于开始和结束时间,分为1小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!