我有2個日期範圍PHP日期範圍檢查
[contract_start_date] => 2011-10-20 [contract_end_date] => 2011-10-29
,我想驗證是否低於日期日期範圍內的範圍以上
2011-05-05 and 2011-10-10
給出的日期,不得以任何方式超出合同範圍
在PHP中是否有這樣的功能?
我有2個日期範圍PHP日期範圍檢查
[contract_start_date] => 2011-10-20 [contract_end_date] => 2011-10-29
,我想驗證是否低於日期日期範圍內的範圍以上
2011-05-05 and 2011-10-10
給出的日期,不得以任何方式超出合同範圍
在PHP中是否有這樣的功能?
見:: http://php.net/manual/en/datetime.diff.php
$datetime1 = new DateTime('2011-10-20');
$datetime2 = new DateTime('2011-10-29');
//PHP 5.3.0
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
//PHP 5.2.2
var_dump($datetime1 < $datetime2);
$datetime3 = new DateTime('2011-05-05');
$datetime4 = new DateTime('2011-10-10');
if ($datetime3 > $datetime1 && $datetime2 > $datetime1 && $datetime3 < $datetime2 && $datetime2 < $datetime2) {
//valid range
}//end if
$start = strtorime($contract_start_date);
$end = strtotime($contract_end_date);
$required_start = strtotime("2011-05-05");
$required_end = strtotime("2011-10-10");
if ($end > $required_end or $end < $required_start)
{
//out of range
}
if ($start < $required_start or $start > $required_end)
{
//out of range
}
這應該給你,你尋找什麼:
define(CONTRACT_START, "2011-10-20");
define(CONTRACT_END, "2011-10-29");
function checkDateRange($dateArray)
{
foreach($dateArray as $dateStr)
{
$curDate = strtotime($dateStr);
if($curDate < strtotime(CONTRACT_START) || $curDate > strtotime(CONTRACT_END))
{
return false;
}
}
return true;
}
$dates = array(0 => "2011-10-02", 1 => "2011-10-25");
if(checkDateRange($dates))
{
echo "Dates are within range";
}
else
{
echo "Dates are NOT within range";
}
找到下面的代碼,以獲得天的時間差,月和年:
<?php
function datediff($date1,$date2,$format='d'){
$difference = abs(strtotime($date2) - strtotime($date1));
switch (strtolower($format)){
case 'd':
$days = round((($difference/60)/60)/24,0);
break;
case 'm':
$days = round(((($difference/60)/60)/24)/30,0);
break;
case 'y':
$days = round(((($difference/60)/60)/24)/365,0);
break;
}
return $days;
}
//Example
echo datediff('2011-06-1','2010-8-30','D') . ' Days<br />';
echo datediff('2011-06-1','2010-8-30','m') . ' Months<br />';
echo datediff('2011-06-1','2010-8-30','y') . ' Years<br />';
?>
可能的重複[How to che如果一個日期是在給定的範圍?](http://stackoverflow.com/questions/976669/how-to-check-if-a-date-is-in-a-given-range) – lonesomeday
但我的有2日期範圍內檢查 – WilfredMifsud
那麼,啊,檢查兩次? – lonesomeday