我有一个数组,每个元素都包含一个在' dd.mm.yy'中形成的startdate和enddate。
Array
(
[0] =
(
[date_start] = 23.07.2014
[date_end] = 02.08.2014
)
)
现在我需要构建一个数组,该数组将包含date_start和date_end之间的每一天,以便稍后验证它们。
现在我的问题是如何计算这些日期之间的每一天并将其保存到数组?
答案 0 :(得分:2)
如果您只是想要差异
<?php
$arr=array
(
array
(
'date_start' =>'23.07.2014',
'date_end' =>'02.08.2014'
),
array
(
'date_start' =>'15.07.2014',
'date_end' =>'02.08.2014'
)
);
foreach($arr as $key=>$val){
$diff=strtotime($val['date_end'])-strtotime($val['date_start']);
$new_arr[]=array(
'day_start'=>$val['date_start'],
'day_end'=>$val['date_end'],
'difference'=>$diff/(24*60*60)-1
);
}
print_r($new_arr);
?>
输出
Array
(
[0] => Array
(
[day_start] => 23.07.2014
[day_end] => 02.08.2014
[difference] => 9
)
[1] => Array
(
[day_start] => 15.07.2014
[day_end] => 02.08.2014
[difference] => 17
)
)
如果你想要在阵列之间的所有日期
<?php
$arr=array
(
array
(
'date_start' =>'23.07.2014',
'date_end' =>'02.08.2014'
),
array
(
'date_start' =>'15.07.2014',
'date_end' =>'02.08.2014'
)
);
foreach($arr as $key=>$val){
$diff=strtotime($val['date_end'])-strtotime($val['date_start']);
$day_diff=$diff/(24*60*60);
$alldiff=array();
for($i=1;$i<=$day_diff-1;$i++){
$alldiff[]=date('d.m.Y',(strtotime($val['date_start'])+$i*24*60*60));
}
$new_arr[]=array(
'day_start'=>$val['date_start'],
'day_end'=>$val['date_end'],
'difference'=>$alldiff
);
}
echo "<pre>";
print_r($new_arr);
?>
输出
Array
(
[0] => Array
(
[day_start] => 23.07.2014
[day_end] => 02.08.2014
[difference] => Array
(
[0] => 24.07.2014
[1] => 25.07.2014
[2] => 26.07.2014
[3] => 27.07.2014
[4] => 28.07.2014
[5] => 29.07.2014
[6] => 30.07.2014
[7] => 31.07.2014
[8] => 01.08.2014
)
)
[1] => Array
(
[day_start] => 15.07.2014
[day_end] => 02.08.2014
[difference] => Array
(
[0] => 16.07.2014
[1] => 17.07.2014
[2] => 18.07.2014
[3] => 19.07.2014
[4] => 20.07.2014
[5] => 21.07.2014
[6] => 22.07.2014
[7] => 23.07.2014
[8] => 24.07.2014
[9] => 25.07.2014
[10] => 26.07.2014
[11] => 27.07.2014
[12] => 28.07.2014
[13] => 29.07.2014
[14] => 30.07.2014
[15] => 31.07.2014
[16] => 01.08.2014
)
)
)
答案 1 :(得分:1)
这应该有效:
$startDate = new DateTime('2014-05-01');
$endDate = new DateTime('2014-06-02');
// calculate diff
$diff = $startDate->diff($endDate);
$daysDiff = $diff->days;
for ($i = 0; $i < $daysDiff; $i++) {
// clone so the original date will not be changed
$currentDate = clone $startDate;
$currentDate->modify('+'.$i.' days');
echo $currentDate->format('Y-m-d H:i:s')."\n";
}
现在你只需要为每个数组条目创建它并将其保存到数组
答案 2 :(得分:0)
为什么不将日期转换为时间戳,然后从timestamp_start循环到timestamp_end,按天数秒,然后将每个步骤转换为新日期以便稍后检查。
答案 3 :(得分:0)
这样的东西?
// Convert start and end dates to unix timestamp
$start = strptime($date_array[0]['date_start'], '%d.%m.%Y');
$end = strptime($date_array[0]['date_end'], '%d.%m.%Y');
$dates = array();
while ($start < $end){
$dates[] = date('d.m.Y', $start);
$start += 86400 // One day equals 86400 seconds
}
答案 4 :(得分:0)
试试这个,因为它适合我
function getDates($startTime, $endTime) {
$day = 86400;
$format = 'd.m.Y';
$startTime = strtotime($startTime);
$endTime = strtotime($endTime);
$numDays = round(($endTime - $startTime) / $day);
$days = array();
for ($i = 1; $i < $numDays; $i++) {
$days[] = date($format, ($startTime + ($i * $day)));
}
return $days;
}
$ days = getDates('23 .07.2014','02 .08.2014');