dptsivmg82908 2015-05-09 07:35
浏览 56
已采纳

php DateTime对象比较

I have simple DateTime lesson. I'm comparing the 2 DateTime objects and they dont work right. I've tried swapping things around and it still fails. It fails when I enter the current date manually, even if the date is the same. I have to enter a date manually though. Thanks for any help. Heres the function:

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
    }
  }

I changed everything to make format comparisons, but now the lowest date echos even when I put in a future date. Heres what happens:

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); 
    }
  }

I've been playing with this, and I got it to work by using some of both. I think this is not the way its supposed to work and might cause problems with some other date now that im unaware of. Heres what I did:

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';
    }
}

</div>
  • 写回答

3条回答 默认 最新

  • douxian0008 2015-05-09 07:39
    关注

    Well you defined your format in which you want to compare your two DateTime's, but you didn't used it.

    Just use format() with your format variable when you compare the two DateTimes's otherwise also secons and minutes, just everything gets compared.

    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
    }
    

    EDIT:

    This should work for you, you have to first format your date and then take the timestamp, like this:

    $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; 
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!