几个日期之间的比较

时间:2016-12-11 22:36:01

标签: php codeigniter date

我的要求:

  • 如果时间是19:30:00至08:00:00 echo 10
  • 否则回显3

我应该如何更改代码以满足这些要求?

DEMO:https://3v4l.org/tpauu

<?php

//$today_dt = new DateTime(date("H:i:s"));
//$today_dt = new DateTime(date("01:00:00")); **** Different result!!
$today_dt = new DateTime(date("01:00:00")); //**** Different result!!
$expire_dt1 = new DateTime(date("19:30:00"));
$expire_dt2 = new DateTime(date("08:00:00"));
if ($today_dt < $expire_dt1 && $today_dt < $expire_dt2){
    $DateEnd = strtotime(date('10:30:00'));
    echo '10';
}else{
    $DateEnd = strtotime(date('H:i:s')." +3 hour");
    echo '3';
}
echo '<br>'.date('H:i:s',$DateEnd);

?>

1 个答案:

答案 0 :(得分:0)

新的一天开始...... $today_dt = new DateTime(date("01:00:00"));首先是$expire_dt2 = new DateTime(date("08:00:00"));然后是$expire_dt1 = new DateTime(date("19:30:00"));所以我认为你必须进行适当的比较。 检查一下 var_dump($today_dt<$expire_dt1); var_dump($today_dt<$expire_dt2);两者都给出了真var_dump($expire_dt1>$expire_dt2); also gives true.So makes the following comparison ..hope it will works..

//$today_dt = new DateTime(date("H:i:s"));
//$today_dt = new DateTime(date("01:00:00")); **** Different result!!
$today_dt = new DateTime(date("01:00:00")); //**** Different result!!
$expire_dt1 = new DateTime(date("19:30:00"));
$expire_dt2 = new DateTime(date("08:00:00"));
// var_dump($today_dt<$expire_dt1);
// var_dump($today_dt<$expire_dt2);
// var_dump($expire_dt1>$expire_dt2);
if ($today_dt > $expire_dt1 && $today_dt < $expire_dt2){
    $DateEnd = strtotime(date('10:30:00'));
    echo '10';
}else{
    $DateEnd = strtotime(date('H:i:s')." +3 hour");
    echo '3';
}
echo '<br>'.date('H:i:s',$DateEnd);

在此处查看更多http://php.net/manual/en/datetime.diff.php