需要通过开始 - 结束日期循环帮助
我有一个事件日历的开始和结束日期如下:需要通过开始 - 结束日期循环帮助
16.08.2010 12:00:00
- 21.08.2010 20:00:00
16.08.2010 20:00:00
- 21.08.2010 23:00:00
18.08.2010 17:00:00
- 18.08.2010 19:00:00
每当一个事件去在一天之内,我需要循环每一天。
我发现这个线程,我想到的是能帮助我:How to find the dates between two specified date?
我不能使用PHP 5.3的解决方案,因为我的服务器上运行PHP 5.2。
其他解决方案不产生输出。
这是我尝试做:
$events = $data['events'];
foreach($ev as $e) :
$startDate = date("Y-m-d",strtotime($e->startTime));
$endDate = date("Y-m-d",strtotime($e->endTime));
for($current = $startDate; $current <= $endDate; $current += 86400) {
echo '<div>'.$current.' - '.$endDate.' - '.$e->name.'</div>';
}
endforeach;
从理论上讲,这应该遍历所有天延伸数天的事件。 但这没有发生。
的逻辑是错误的地方....请帮助:)
的问题是,你要号码添加到字符串。 date('Y-m-d')
产生一个像2011-01-31
这样的字符串。向它添加数字将不起作用[如预期]:'2011-01-31' + 86400 = ?
。
尝试一些沿着这些路线:
// setting to end of final day to avoid glitches in end times
$endDate = strtotime(date('Y-m-d 23:59:59', strtotime($e->endTime)));
$current = strtotime($e->startTime);
while ($current <= $endDate) {
printf('<div>%s - %s - %s</div>', date('Y-m-d', $current), date('Y-m-d', $endDate), $e->name);
$current = strtotime('+1 day', $current);
}
日期( “Y-M-d”)是错误的,你需要在for循环的strtotime结果。尝试这个,它应该工作:
$events = array(
array('16.08.2010 12:00:00', '21.08.2010 20:00:00', 'event1'),
array('16.08.2010 20:00:00', '21.08.2010 23:00:00', 'event2'),
array('18.08.2010 17:00:00', '18.08.2010 19:00:00', 'event3'),
);
$dayLength = 86400;
foreach($events as $e) :
$startDate = strtotime($e[0]);
$endDate = strtotime($e[1]);
if(($startDate+$dayLength)>=$endDate) continue;
for($current = $startDate; $current <= $endDate; $current += $dayLength) {
echo '<div>'.date('Y-m-d', $current).' - '.date('Y-m-d', $endDate).' - '.$e[2].'</div>';
}
endforeach;
好吧,我还不够快...... :) – Marc 2011-01-31 00:23:42
现货!谢谢:) – Steven 2011-01-31 00:18:58