我每天必须得到一份事件清单:
$data = getEvents();
$content='';
foreach($data as $d)
{
$content.= $d['date'].' '.$d['ID_Event']."<br />";
}
echo $content;
------->
2015-03-20 1
2015-03-22 2
2015-03-23 3
我必须检查是否在这个列表中,我在每两个日期之间有一个空的日子,然后放一个id = 4的默认事件,所以我有这个结果
2015-03-20 1
**2015-03-21 4**
2015-03-22 2
2015-03-23 3
由于
答案 0 :(得分:1)
这应该适合你:
(这里我首先按usort()
的日期对$data
数组进行排序。之后我为开始和结束之间的所有日期创建new DatePeriod()
。我只是简单地循环所有日期,如果它在$data
数组中,我将其添加到$output
数组,否则我使用EventID 4创建一个元素。最后我只是打印数据)
<?php
$data = getEvents();
usort($data, function($a, $b){
return strtotime($a["date"]) > strtotime($b["date"]);
});
$start = new DateTime($data[0]["date"]);
$interval = new DateInterval('P1D');
$end = (new DateTime($data[count($data)-1]["date"]))->add($interval);
$period = new DatePeriod($start, $interval, $end);
foreach($period as $date) {
if(($key = array_search($date->format("Y-m-d"), array_column($data, "date"))) !== FALSE)
$content[] = $data[$key];
else
$content[] = ["date" => $date->format("Y-m-d"), "ID_Event" => "4"];
}
array_map(function($v){
echo $v['date'] . " " . $v['ID_Event'] . "<br />";
}, $content);
?>
答案 1 :(得分:0)
$data = getEvents();
$default = 4;
$content='';
foreach($data as $index=>$d)
{
$date = new DateTime($d['date']);
$content.= $d['date'].' '.$d['ID_Event']."<br />";
//check if we have a next event
if (isset($data[$index+1])){
//get the date of next event
$next = new DateTime($data[$index+1]['date']);
//how many days till next event
$difference = $date->diff($next)->d;
//more then 1 day in between two events
if($difference > 1){
//loop through each day difference
for($i = 2; $i <= $difference; $i++){
//take the last even date and add one day to it
$date->add(new DateInterval('P1D'));
//add the new date to content with your default event
$content.= $date->format('Y-m-d').' '.$default."<br />";
}
}
}
}
echo $content;