假设我的开始日期为22-05-2019,结束日期为15-08-2019
我如何计算这两个日期之间的日期,而仅计算8月到7月之间的月份?
答案 0 :(得分:0)
这应该回答您的问题
$start = strtotime("22-05-".date("Y"));
$end = strtotime("15-08-".date("Y"));
$july = strtotime("01-07-".date("Y"));
$august = strtotime("31-08-".date("Y"));
if($july>$start){
$start = $july;
}
if($end > $august){
$end= $august;
}
$datediff = $end - $start;
echo round($datediff / (60 * 60 * 24));
答案 1 :(得分:0)
您可以将日期转换为时间戳,并可以像差值时间戳一样设置数字天:
$timestampstart = strtotime('22-05-2019');
$timestampend = strtotime('15-08-2019');
$days= ( $timestampend - $timestampstart ) /3600/24;
echo "days " . $days;
答案 2 :(得分:0)
这是一个可以作为函数使用的解决方案(因此您可以随时调用它),并且可以在任何日期或删除的月份中使用。
function remMon($d1, $d2, $array) {
$months = array(31,28,31,30,31,30,31,31,30,31,30,31);
array_unshift($months, 0);
unset($months[0]);
$removed = array_sum(array_intersect_key($months,array_flip($array)));
$date1 = strtotime($d1);
$date2 = strtotime($d2);
if(in_array(intval(date("m", $date1)),$array)){
$removed = $removed - (date("d", $date1));
}
if(in_array(intval(date("m", $date2)),$array)){
$removed = $removed - ($months[intval(date("m", $date2))] - date("d", $date2));
}
$datediff = $date2 - $date1;
$final = round($datediff / (60 * 60 * 24)) - $removed;
return $final;
}
示例:
echo 'DAYS:' . remMon("22-May-2019","15-August-2019",array(5,6));
DAYS:46