使用m-d-Y格式比较日期


Comparing dates using m-d-Y format

我有日期$current, $start和$end格式为m-d-Y。我想要一个条件其中,如果当前日期在开始和结束日期之间,它将显示"当前",但我就是不明白为什么在世界上它不会打印我想要的东西。哈哈

示例声明将是

$current = '07-03-2014';
$start = '06-01-2013';
$end = '08-02-2015';
    if(($current > $start) && ($current < $end)) {
    echo "current";
    }
    else {
    "not current";
    }

为什么我有"不电流"作为输出?我做错了什么?我很确定你也有这个问题。: D

可以直接与正确格式Y-m-d比较或使用strtotime()

$current = date('Y-m-d', strtotime('07-03-2014'));
 $start = date('Y-m-d', strtotime('06-01-2013'));
 $end = date('Y-m-d', strtotime('08-02-2015'));
 if(($current > $start) && ($current < $end)) {
    echo "current";
 }
 else {
    "not current";
 }

您可以尝试使用strtotime

$current = '07-03-2014';
$start = '06-01-2013';
$end = '08-02-2015';
    if((strtotime($current) > strtotime($start)) && (strtotime($current) < strtotime($end))) {
    echo "current";
    }
    else {
        echo  "not current";
    }

METHOD: 2

$current = '07-03-2014';
$start = '06-01-2013';
$end = '08-02-2015';

$current_time = strtotime($current);
$start_time = strtotime($start);
$end_time = strtotime($end);
    if(($current_time > $start_time) && ($current_time < $end_time)) {
    echo "current";
    }
    else {
     echo  "not current";
    }

使用strtotime:

$current = '07-03-2014';
$start = '06-01-2013';
$end = '08-02-2015';
    if((strtotime($current) > strtotime($start)) && (strtotime($current) < strtotime($end))) {
    echo "current";
    }
    else {
    "not current";
    }