为什么我的IF语句没有';I don’我不知道中间的日期


Why does my IF statement doesn't catch the date in-between

我的数据库中有许多日期不同的记录,但我的代码没有捕捉到日期,也没有显示出来。怎么了?

这是我的代码

$year = date("Y");
$sem1_s = date("M-d-Y", strtotime(date('Y') . '-8-1 00:00:00'));
$sem1_e = date("M-d-Y", strtotime(date('Y') . '-12-31 00:00:00'));
$sem2_s = date("M-d-Y", strtotime(date('Y') . '-01-1 00:00:00'));
$sem2_e = date("M-d-Y", strtotime(date('Y') . '-06-30 00:00:00'));
$date = '2015-10-15';
$date2 = date("M-d-Y", strtotime(date('Y') . '-3-1 00:00:00'));
if($date2 >= $sem1_s && $date2 <= $sem1_e){
    echo $date2;
    echo "First Sem";
}
else if($date2 >= $sem2_s && $date2 <= $sem2_e){
    echo $date2;
    echo "Second Sem";
}
else{
    echo "error";
}

请在您的if语句中尝试此操作,正如我之前在问题下方的评论中所提到的,问题出在if语句上。所以把它改成这个:

if (strtotime($date2) >= strtotime($sem1_s) && strtotime($date2) <= strtotime($sem1_e))

尝试像这样更改if语句

$year = date("Y");
$sem1_s = date("M-d-Y", strtotime(date('Y') . '-8-1 00:00:00'));
$sem1_e = date("M-d-Y", strtotime(date('Y') . '-12-31 00:00:00'));
$sem2_s = date("M-d-Y", strtotime(date('Y') . '-01-1 00:00:00'));
$sem2_e = date("M-d-Y", strtotime(date('Y') . '-06-30 00:00:00'));
$date = '2015-10-15';
$date2 = date("M-d-Y", strtotime(date('Y') . '-3-1 00:00:00'));
if($date2 >= $sem1_s || $date2 <= $sem1_e){
    echo $date2;
    echo "First Sem";
}
else if($date2 >= $sem2_s || $date2 <= $sem2_e){
   echo $date2;
   echo "Second Sem";
}
else{
    echo "error";

}

我认为不能在字符串中使用大于或小于符号您应该将变量转换为strtotime。

    if(strtotime($date2) >= strtotime($sem1_s) && strtotime($date2) <= strtotime($sem1_e)){
    echo $date2;
    echo "First Sem";
    }
    else if(strtotime($date2) >= strtotime($sem2_s) && strtotime($date2) <= strtotime($sem2_e)){
     echo $date2;
    echo "Second Sem";
    }
    else{
    echo "error";

因为需要将日期转换为UNIX时间戳进行比较。

以下将返回所需结果

if(strtotime($date2) >= strtotime($sem1_s) && strtotime($date2) <= strtotime($sem1_e)){
   echo $date2;
   echo "First Sem";
}
else if(strtotime($date2) >= strtotime($sem2_s) && strtotime($date2) <= strtotime($sem2_e)){
   echo $date2;
   echo "Second Sem";
}
else{
   echo "error";
}
相关文章: