php DateTime对象比较

时间:2015-05-09 07:35:42

标签: php date datetime

我有简单的DateTime课程。我比较了2个DateTime对象,但它们无法正常工作。我试过交换周围的东西但它仍然失败。当我手动输入当前日期时,即使日期相同,它也会失败。我必须手动输入日期。谢谢你的帮助。继承人的职责:

function dtcompare(){
    //I make the date the same as the current 
    //date and it doesnt echo that its equal. 
    $date1 = new DateTime('5/9/2015'); //a date entered
    $date2 = new DateTime(); //the current date
    $mdy = 'n/j/Y'; //the format

    if($date1 == $date2){

        echo "d1 == d2"; //This should be echoing
    }
    elseif($date1  > $date2){

        echo "d1 > d2";
    }
    elseif($date1 < $date2){        

        echo "    d1 < d2    "; //but this always echos
    }
  }

我更改了所有内容以进行格式比较,但现在即使我在将来的日期中也是最低的日期回声。继承人会发生什么:

&#13;
&#13;
function dtcompare(){
    
     
    $date1 = new DateTime('5/18/2015'); //a future date
    $date2 = new DateTime(); //the current date
    $mdy = 'n/j/Y'; //the format
    
    if($date1->format($mdy) == $date2->format($mdy)){
   
        echo "d1 == d2";
    }
    elseif($date1->format($mdy)  > $date2->format($mdy)){
        //This will echo if the date is in the next month 6/1/2015    
        echo "d1 > d2";
    }
    elseif($date1->format($mdy) < $date2->format($mdy)){        
   
        //This echos even though it's not 5/18/2015 yet!        
        echo $date1->format($mdy)."d1 < d2".$date2->format($mdy); 
    }
  }
&#13;
&#13;
&#13;

我一直在玩这个,我用它们中的一些来完成它。我认为这不是它应该工作的方式,并且可能会导致其他日期的问题,因为我不知道。继承人我做了什么:

&#13;
&#13;
function dtcompare(){
    
    $date1 = new DateTime('5/12/2015'); //a future date
    $date2 = new DateTime(); //the current date
    $mdy = 'n/j/Y'; //the format
  
  
    //Using the datetime->format for == comparisons and the other
    //way for datetimes greater or less than. This works, but I think this is NOT
    //how this should work though. It feels like a rig for something that
    //should work one way (DateTime->format() comparison) or another (DateTime comparison w.o format)
    if($date1->format($mdy) == $date2->format($mdy)){
        echo 'date1 and date2 have the same date and time';
    }
    elseif($date1 > $date2){
        echo 'date1 > date2 meaning its a later date and time';
    }
    elseif($date1 < $date2){
        echo 'date1 < date2 meaning its an earlier date and time';
    }
}
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:3)

您已经定义了要比较两个DateTime的格式,但是您没有使用它。

当你比较两个DateTimes时,只需使用format()和你的格式变量,否则就是secons和分钟,只需要比较一切。

if($date1->format($mdy) == $date2->format($mdy)){
       //^^^^^^^^^^^^^^ See here, to just compare month, day and year
    echo "d1 == d2"; //This should be echoing
}
elseif($date1->format($mdy)  > $date2->format($mdy)){

    echo "d1 > d2";
}
elseif($date1->format($mdy) < $date2->format($mdy)){        

    echo "    d1 < d2    "; //but this always echos
}

修改

这应该对你有用,你必须首先格式化你的日期然后采取时间戳,如下所示:

$mdy = 'n/j/Y'; //the format
$date1 = strtotime((new DateTime('5/18/2015'))->format($mdy));
$date2 = strtotime((new DateTime())->format($mdy));


if($date1 == $date2){
    echo "d1 == d2";
} elseif($date1  > $date2) {
    echo "d1 > d2";
} elseif($date1 < $date2){        
    echo $date1."d1 < d2".$date2; 
}

答案 1 :(得分:1)

首先使用Date的格式然后进行比较,因为您无法直接比较日期的2个对象

使用:

   <?php

  //I make the date the same as the current 
    //date and it doesnt echo that its equal. 
    $date1 = new DateTime('5/9/2015'); //a date entered
    $date2 = new DateTime(); //the current date
    $mdy = 'n/j/Y'; //the format

$date1New = $date1->format('d-m-y');
$date2New = $date2->format('d-m-y');

////echo '<br> --2'.$date2;

    if($date1New == $date2New){

        echo "d1 == d2"; //This should be echoing
    }
    elseif($date1New  > $date2New){

        echo "d1 > d2";
    }
    elseif($date1New < $date2New){        

        echo "    d1 < d2    "; //but this always echos
    }

   ?>

答案 2 :(得分:0)

php DateTime已经能够输出unixtimestamp。所以你可以这样比较:

$early = new DateTime( "now" );
$later = new DateTime( "now + 2days" );

if ( $early->format('U') < $later->format('U') ) {
    echo "you'll get this printed";
}