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条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀