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

user2933671
  • 273
  • 3
  • 5
  • 19
  • 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. – Marc B Nov 15 '13 at 19:08
  • possible duplicate of [PHP - Compare Date](http://stackoverflow.com/questions/2113940/php-compare-date) And all these http://stackoverflow.com/search?q=+is%3Aquestion+%5Bphp%5D+compare+dates – Mike B Nov 15 '13 at 19:11

2 Answers2

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...
}
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
  • 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 Nov 15 '13 at 19:11
  • @user2933671: That's not how you concatenate strings in PHP. Use `.` instead of `+`. Read the [documentation](http://php.net/manual/en/language.operators.string.php). – Amal Murali Nov 15 '13 at 19:14
  • @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! – user2933671 Nov 15 '13 at 19:25
  • @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 – user2933671 Nov 15 '13 at 19:27
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);
Viacheslav Kondratiuk
  • 8,493
  • 9
  • 49
  • 81