Compare Two dates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maheswaran
    New Member
    • Mar 2007
    • 190

    Compare Two dates

    Hi all,

    How to compare the two dates...? See my code

    Code:
    while(list($cdate)=mysql_fetch_array($res))
    {
    echo "$i.cur date is ".$cdate;
    if($pdate==$cdate)
    echo "previous record date and current record date is same";
    }
    else
    {
    echo "not same":
    }
    echo "- date pre is ". $pdate=$cdate;
    echo "<br>";
    $i++
    }
    i got

    1. cur date is 2009-08-22 - pre date is empty for first loop
    2. cur date is 2009-08-23 - pre date is 2009-08-22
    3. cur date is 2009-08-24 - pre date is 2009-08-23
    4. cur date is 2009-08-25 - pre date is 2009-08-24

    What i did i checked current record and previous record dates whether they are same or not for whole result (say have ten records).

    but the if condtion is not work for date even am checked with < and > conditions but not get succeed.
    And also tried with strtotime option... when am using this function to both pdate and $cdate varibales its working only $cdate value, $pdate returns only empty value.. i don't know why...

    Any body help me>>>
  • didoamylee
    New Member
    • Nov 2008
    • 16

    #2
    I'm a little confused.
    Is $cdate the current date?
    Do you store the current date and the previous date in the database?

    The way i would do this:
    -i would store the date in only one field in the database
    -then i would compare the database date with $cdate = date("Y-m-d",time()); // $cdate is now the current date

    Comment

    • maheswaran
      New Member
      • Mar 2007
      • 190

      #3
      Hi dido,

      Just i compared current record date (not current date) and previous record date whether they are same or not. Current record date may any date and am not stored both dates, i stored only current record date , i assigned this current record date value into previous_record _date_val and i checked both values are equal.

      1. current record date is 2009-08-22 - previous record date is empty for first loop
      2. current record date is 2009-08-23 - previous record date is 2009-08-22
      3. current record date is 2009-08-24 - previous record date is 2009-08-23
      4. current record date is 2009-08-25 - previous record date is 2009-08-24

      and see my code aboove
      Is there any other simple way to compare dates?

      Comment

      • paulrajj
        New Member
        • Sep 2008
        • 47

        #4
        Where u have defined the $pdate ? can u show us the code ?

        Comment

        Working...