PHP - 如何在日期数组中查找缺失的日期并填补缺失的空白
PHP - How to find missing dates in array of dates and fill missing gap
你好 Whosebug 的伟大社区。我有一个我无法解决的问题。我需要在 X 个日期之间找到缺失的日期。谢谢
正在从数据库获取数据
$stmt=$db_con->prepare("SELECT date_from, date_to, country FROM work WHERE worker_id=xx");
while($row = $stmt->fetch()) { <br>
...
...
data = array("date_from" => $date_from, "date_to" => $date_to, "country" => $country);
}
我得到的数据是这样的
15.01.2020 --- 20.01.2020 --- 国家 0
01.02.2020 --- 05.02.2020 --- 国家 1
10.02.2020 --- 20.02.2020 --- 国家 2
这些日期是指工人在国外开始和结束工作的时间。我需要获取日期以了解工人何时在本国工作。第一个日期之前,最后一个日期之后以及中间的日期表示在本国工作。我的最终数据应该像这样保存在数组中:
01.01.2020 --- 14.01.2020 --- 主页
15.01.2020 --- 20.01.2020 --- 国家 0
21.01.2020 --- 31.01.2020 --- 主页
01.02.2020 --- 05.02.2020 --- 国家 1
6.02.2020 --- 09.02.2020 --- 主页
10.02.2020 --- 20.02.2020 --- 国家 2
21.02.2020 --- 31.12.2020 --- 主页
可以通过PHP
实现
// First day of the month.
echo date('Y-m-01', strtotime($query_date));
// Last day of the month.
echo date('Y-m-t', strtotime($query_date));
例如:
while($row = $stmt->fetch()) {
$first_day = date('Y-m-01', strtotime($date_from));
$before_date_from= date('Y-m-d', strtotime($date_from." - 1 day"));
$last_day = date('Y-m-t', strtotime($date_from));
$after_date_to = date('Y-m-d', strtotime($date_to." + 1 day"));
$data[] = array("date_from" => $first_day, "date_to" => $before_date_from, "country" => "Home");
$data[] = array("date_from" => $date_from, "date_to" => $date_to, "country" => $country);
$data[] = array("date_from" => $after_date_to, "date_to" => $last_day, "country" => "Home");
}
print_r($data); // check your result here..
我认为您示例中的 2019 是一个拼写错误,因为数组的其余部分排序正确。
在这里,我首先要确保第一个项目的日期是该月的第一天。如果不是,则它会添加该月的 "start"。
然后它进入循环并检查项目之间是否有间隙,如果有间隙则添加一个主页项目。
$arr = array ( 0 => array ( 'datum_od' => '01.01.2020', 'datum_do' => '10.01.2020', ), 1 => array ( 'datum_od' => '15.01.2020', 'datum_do' => '20.01.2020', ), 2 => array ( 'datum_od' => '01.02.2020', 'datum_do' => '05.02.2020', ), 3 => array ( 'datum_od' => '25.11.2020', 'datum_do' => '25.12.2020', ));
$end_date = 0;
if(date('d', strtotime($arr[0]['datum_od'])) != '01'){
$end_date = date('d.m.Y', strtotime($arr[0]['datum_od'] . ' -1 day'));
$new[] = array('datum_od' => date('01.m.Y', strtotime($arr[0]['datum_od'])), 'datum_do' => $end_date, 'home');
}
foreach($arr as $key => $item){
if(date("d.m.Y", strtotime($end_date . " +1 day")) != date('d.m.Y', strtotime($item['datum_od']))){
$end_date = date('d.m.Y', strtotime($item['datum_od'] . ' -1 day'));
$new[] = array('datum_od' => date('d.m.Y', strtotime($arr[$key-1]['datum_do'] . " +1 day")), 'datum_do' => $end_date, 'home');
$new[] = $item;
}else{
$new[] = $item;
}
}
var_dump($new);
你好 Whosebug 的伟大社区。我有一个我无法解决的问题。我需要在 X 个日期之间找到缺失的日期。谢谢
正在从数据库获取数据
$stmt=$db_con->prepare("SELECT date_from, date_to, country FROM work WHERE worker_id=xx");
while($row = $stmt->fetch()) { <br>
...
...
data = array("date_from" => $date_from, "date_to" => $date_to, "country" => $country);
}
我得到的数据是这样的
15.01.2020 --- 20.01.2020 --- 国家 0
01.02.2020 --- 05.02.2020 --- 国家 1
10.02.2020 --- 20.02.2020 --- 国家 2
这些日期是指工人在国外开始和结束工作的时间。我需要获取日期以了解工人何时在本国工作。第一个日期之前,最后一个日期之后以及中间的日期表示在本国工作。我的最终数据应该像这样保存在数组中:
01.01.2020 --- 14.01.2020 --- 主页
15.01.2020 --- 20.01.2020 --- 国家 0
21.01.2020 --- 31.01.2020 --- 主页
01.02.2020 --- 05.02.2020 --- 国家 1
6.02.2020 --- 09.02.2020 --- 主页
10.02.2020 --- 20.02.2020 --- 国家 2
21.02.2020 --- 31.12.2020 --- 主页
可以通过PHP
实现// First day of the month.
echo date('Y-m-01', strtotime($query_date));
// Last day of the month.
echo date('Y-m-t', strtotime($query_date));
例如:
while($row = $stmt->fetch()) {
$first_day = date('Y-m-01', strtotime($date_from));
$before_date_from= date('Y-m-d', strtotime($date_from." - 1 day"));
$last_day = date('Y-m-t', strtotime($date_from));
$after_date_to = date('Y-m-d', strtotime($date_to." + 1 day"));
$data[] = array("date_from" => $first_day, "date_to" => $before_date_from, "country" => "Home");
$data[] = array("date_from" => $date_from, "date_to" => $date_to, "country" => $country);
$data[] = array("date_from" => $after_date_to, "date_to" => $last_day, "country" => "Home");
}
print_r($data); // check your result here..
我认为您示例中的 2019 是一个拼写错误,因为数组的其余部分排序正确。
在这里,我首先要确保第一个项目的日期是该月的第一天。如果不是,则它会添加该月的 "start"。
然后它进入循环并检查项目之间是否有间隙,如果有间隙则添加一个主页项目。
$arr = array ( 0 => array ( 'datum_od' => '01.01.2020', 'datum_do' => '10.01.2020', ), 1 => array ( 'datum_od' => '15.01.2020', 'datum_do' => '20.01.2020', ), 2 => array ( 'datum_od' => '01.02.2020', 'datum_do' => '05.02.2020', ), 3 => array ( 'datum_od' => '25.11.2020', 'datum_do' => '25.12.2020', ));
$end_date = 0;
if(date('d', strtotime($arr[0]['datum_od'])) != '01'){
$end_date = date('d.m.Y', strtotime($arr[0]['datum_od'] . ' -1 day'));
$new[] = array('datum_od' => date('01.m.Y', strtotime($arr[0]['datum_od'])), 'datum_do' => $end_date, 'home');
}
foreach($arr as $key => $item){
if(date("d.m.Y", strtotime($end_date . " +1 day")) != date('d.m.Y', strtotime($item['datum_od']))){
$end_date = date('d.m.Y', strtotime($item['datum_od'] . ' -1 day'));
$new[] = array('datum_od' => date('d.m.Y', strtotime($arr[$key-1]['datum_do'] . " +1 day")), 'datum_do' => $end_date, 'home');
$new[] = $item;
}else{
$new[] = $item;
}
}
var_dump($new);