所以,我在两个数组中有一些日期:
$old_1 = array(
array(
'start_date' => '2014-09-06',
'end_date' => ' 2014-09-07'
),
array(
'start_date' => '2014-09-26',
'end_date' => '2014-09-28'
),
array(
'start_date' => '2014-09-29',
'end_date' => '2014-10-02'
),
array(
'start_date' => '2014-10-03',
'end_date' => ' 2014-10-04'
),
array(
'start_date' => '2014-10-06',
'end_date' => ' 2014-10-23'
),
array(
'start_date' => '2014-11-19',
'end_date' => '2014-11-23'
),
array(
'start_date' => '2014-11-25',
'end_date' => ' 2014-11-28'
)
);
$new_1 = array(
array(
'start_date' => '2014-10-03',
'end_date' => ' 2014-10-04'
),
array(
'start_date' => '2014-10-10',
'end_date' => ' 2014-10-12'
),
array(
'start_date' => '2014-10-17',
'end_date' => '2014-10-19'
),
array(
'start_date' => '2014-11-19',
'end_date' => ' 2014-11-23'
),
array(
'start_date' => '2014-11-25',
'end_date' => '2014-11-28'
)
);
我正在比较数组$ old_1和数组$ new_1,查找日期不在数组$ old_1中。我期待的结果是:
$expected_1 = array(
array(
array(
'start_date' => '2014-10-10',
'end_date' => ' 2014-10-12'
),
array(
'start_date' => '2014-10-17',
'end_date' => '2014-10-19'
)
);
我使用此功能来执行此操作:
function checkDate($redDate, $date_range)
{
$start_object = DateTime::createFromFormat('Y-m-d', $redDate['start_date']);
$end_object = DateTime::createFromFormat('Y-m-d', $redDate['end_date']);
if (is_array($date_range)) {
foreach ($date_range as $key => $row) {
$range_start = DateTime::createFromFormat('Y-m-d', $row['start_date']);
$range_end = DateTime::createFromFormat('Y-m-d', $row['end_date']);
if ($start_object != $range_start && $end_object != $range_end) {
if ($start_object < $range_end && $end_object > $range_start) {
$date = $redDate;
}
}
}
}
return $date;
}
$expected = array();
foreach ($new_1 as $dates) {
$value = checkDate($dates, $old_1);
if (!empty($value)) {
$expected[] = $value;
}
}
但是这个功能对这两个数组不起作用:
$old_2 = array(
array(
'start_date' => '2014-09-26',
'end_date' => '2014-09-28'
),
array(
'start_date' => '2014-10-03',
'end_date' => ' 2014-10-04'
)
);
$new_2 = array(
array(
'start_date' => '2014-09-26',
'end_date' => '2014-09-28'
),
array(
'start_date' => '2014-10-03',
'end_date' => ' 2014-10-04'
),
array(
'start_date' => '2014-10-17',
'end_date' => '2014-10-20'
),
array(
'start_date' => '2014-10-29',
'end_date' => ' 2015-01-02'
)
);
我在期待:
$expected_2 = array(
array(
array(
'start_date' => '2014-10-17',
'end_date' => '2014-10-20'
),
array(
'start_date' => '2014-10-29',
'end_date' => ' 2015-01-02'
)
);
但我得到了空数组:
$result_2 = array(
);
在比较功能中我会错过什么吗? 我需要你的建议,谢谢:)
答案 0 :(得分:0)
试试这个checkDate
功能:
function checkDate($redDate, $date_range)
{
$start_object = DateTime::createFromFormat('Y-m-d', $redDate['start_date']);
$end_object = DateTime::createFromFormat('Y-m-d', $redDate['end_date']);
if (is_array($date_range)) {
foreach ($date_range as $key => $row) {
$range_start = DateTime::createFromFormat('Y-m-d', $row['start_date']);
$range_end = DateTime::createFromFormat('Y-m-d', $row['end_date']);
// if the date was found return a null object
if ($start_object == $range_start && $end_object == $range_end) {
return null;
}
}
}
// the date was not found so return the date
return $redDate;
}
答案 1 :(得分:0)
您的示例中的某些日期中有空格。这是一个你应该纠正的错误。
在这里你有内在的痕迹。
2014-09-26 < 2014-10-04 TRUE && 2014-09-28 > 2014-10-03 FALSE
2014-10-03 < 2014-09-28 FALSE && 2014-10-04 > 2014-09-26 TRUE
2014-10-17 < 2014-09-28 FALSE && 2014-10-20 > 2014-09-26 TRUE
2014-10-17 < 2014-10-04 FALSE && 2014-10-20 > 2014-10-03 TRUE
2014-10-29 < 2014-09-28 FALSE && 2015-01-02 > 2014-09-26 TRUE
2014-10-29 < 2014-10-04 FALSE && 2015-01-02 > 2014-10-03 TRUE
array(0) { }
无论如何,我并不确切地知道你在寻找什么。你想要实现什么目标?