有这样一个需求,给定一个时刻c,计算这个时刻两小时后的时刻是多少,12:00到13:30这90分钟不计时
分3种情况:
<?php
function getDeadline($c) {
$timeBetween = ['Y-m-d 12:00:00','Y-m-d 13:30:00'];
$a = date($timeBetween[0]);
$b = date($timeBetween[1]);
// 2小时后
$d = date('Y-m-d H:i:s', strtotime('+2 hour', strtotime($c)));
// 情况1:
if($d >= $a && $d <= $b){
$diff = strtotime($d) - strtotime($a);
return date('Y-m-d H:i:s', strtotime('+'.$diff.' second', strtotime($b)));
}
// 情况2:
if ($c < $a && $d > $b) {
return date('Y-m-d H:i:s', strtotime('+90 minute', strtotime($d)));
}
// 情况3:
if ($c >= $a && $c <= $b) {
return date('Y-m-d 15:30:00');
}
return $d;
}
$lastEditedDate = date('Y-m-d H:i:s', strtotime('2020-06-24 12:40:00'));
$deadline = getDeadline($lastEditedDate);
#echo date_format($lastEditedDate,"Y-m-d H:i:s");
#echo date_format($deadline,"Y-m-d H:i:s");
echo "\n";
echo $lastEditedDate;
echo "\n";
echo $deadline;
?>
PHP在线运行环境:c.runoob.com/compile/1