0

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

0

3 Answers 3

4

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; 
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thanks for the reply. I updated with what you suggest, but the DateTime object says the greater date (5/18/2015) is lower than today's date (5/9/2015). This same comparison works if I compare w.o using $date->format(). Shouldnt it work either way?
@Zapp Yes just format it as you want the dates and then you can compare them
@Zapp updated my answer now. Before the order of your format made a difference, because it compared the string, so now just take the timestamp and it should all work fine
This works, but those arent DateTime objects. Thanks alot for the help.
@Zapp No, the format() returns a string, and then the strtotime returns an int. So you end up with a integer. If you still want to save the DateTime object. just save the format and strtotime in another variable
|
1

Use formatting of Date first and then compare, as you cant compare 2 objects of date Directly

Use:

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

   ?>

1 Comment

This same comparison works if I compare w.o using $date->format(). But that causes my original problem with == comparison. Shouldnt it work either way?
0

php DateTime already has the ability to output a unixtimestamp. So you can just compare like this:

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

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

1 Comment

Thanks. I didnt use the 'U' option. Does that work with the rest of the comparisons?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.