1

I have two dates in the format YYYY-MM-DD and I want to compare them do do some task.

I tried this piece of code:

         $temp_date = strtotime($cu['date']);
         $temp_upto = strtotime($upto_date);

         if($temp_date <= $temp_upto){
            echo "<tr id='highlight'>"; 
        }else{
            echo "<tr>";
            }

$cu is an array and contains date in the same format. Is this the correct way to compare dates in php? The output which I get is quite weird! I haven't posted the full code because its too long and irrelevant for this question. Thanks!.

UPDATE: So this is what I am trying to do. I have many normal dates and I have a upto date if the normal dates are less than or equal to upto date then I want the id="highlight" added to . THe whole thing is inside a loop for some other purpose of my code.

I echoed the dates like this:

 echo "DATE: " . $temp_date + " UPTO: " . $upto_date;

and the output was something like

DATE: 1349042400 UPTO: 00-00-0000
DATE: 1349388000 UPTO: 00-00-0000
DATE: 1352588400 UPTO: 00-00-0000
DATE: 1352761200 UPTO: 00-00-0000
DATE: 1353193200 UPTO: 00-00-0000
DATE: 1353366000 UPTO: 1353193200
DATE: 1354143600 UPTO: 1353193200
DATE: 1354662000 UPTO: 1353193200

still the comparison doesn't happen. and I dont see any change!

People who are suggestion already answered questions, I have already read and implemented most of them without any success.

UPDATE 2: While I am still trying to figure out my issue. I want to share the expanded code snippet here http://pastebin.com/nsVGV9dg

2
  • if you compared the strtotime values directly, it'd work. those are simple integers and >/</= type comparisons work as expected. You're converting to strings, in a least-significant-value-first ordering, so your comparisons are garbage. Commented Nov 15, 2013 at 19:08
  • possible duplicate of PHP - Compare Date And all these stackoverflow.com/… Commented Nov 15, 2013 at 19:11

2 Answers 2

4

Nope, this isn't right. You're comparing two date strings. You want to compare the timestamps instead:

$temp_dateTS = strtotime($cu['date']);
$temp_uptoTS = strtotime($upto_date);

if ($temp_dateTS <= $temp_uptoTS) {
    # code...
}

This is also possible (and better) if you use DateTime class:

$temp_date = new DateTime($cu['date']);
$temp_upto = new DateTime($upto_date);

if ($temp_date <= $temp_upto) {
    # code...
}
Sign up to request clarification or add additional context in comments.

4 Comments

please check update in my question..I get this error when I use DateTime...Object of class DateTime could not be converted to int
@user2933671: That's not how you concatenate strings in PHP. Use . instead of +. Read the documentation.
@MikeB Yes you are right! there are quite a lot of answers to such an question and I am trying to implement them in my code since an hour now!. I am trying just to find why the hell my comparison doesn't work even if the method is correct!
@AmalMurali I have updated my echo call in OP. I will try to share more of my code in few minutes because I will have to clear aup some unnecessary junk
3

I'm not directly answering your question but I thinks it's better to user php DateTime object.

$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);

2 Comments

He isn't calculating the time difference anywhere.
He can calculate difference and compare it. Don't see a problem.

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.