Urgent Help need on php date comparison!

Discussion in 'PHP' started by afridy, Feb 29, 2012.

  1. #1
    Hai folks,

    my date comparison does not work. pls help!
    <?php
    echo date("Y/m/d");
    //above code Shows 2012/02/29 at the time of running this script. 
    //pls keep in mind and kindly look at my below code.
    
    $date=strtotime("+5 days");
    $udate="2012/3/5"; // user entered date
    list($y,$m,$d) = explode('/', $udate);
    $udate = mktime(0, 0, 0, $m, $d, $y);
    if ($date==$udate){
       echo "yes";
    }else{
       echo "no";
    }
    ?>
    PHP:
    result shows 'no'.

    * this month is february and finishes in 29
     
    afridy, Feb 29, 2012 IP
  2. afridy

    afridy Well-Known Member

    Messages:
    810
    Likes Received:
    116
    Best Answers:
    0
    Trophy Points:
    135
    #2
    ooops, i just noticed that the counter is increasing every second. so now i think this code may be correct folks

    
    <?php
    //echo date("Y/m/d"); // SOWS 2012/02/29 at the time of running this script
    
    $date=strtotime("+5 days");
    $udate="2012/3/6";
    list($y,$m,$d) = explode('/', $udate);
    $udate = mktime(0, 0, 0, $m, $d, $y);
    //echo $date . "-" . $udate . "<br>";
    echo $udate-$date;
    if ((($udate-$date) > 24*60*60) || (($udate-$date) < 0))  { //24*60*60 is the num of seconds in a day
       echo "no";
    }else{
       echo "yes";
    }
    ?>
    PHP:
     
    afridy, Feb 29, 2012 IP
  3. GMF

    GMF Well-Known Member

    Messages:
    855
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #3
    EDIT: Should have refreshed before responding....:eek:

    Well, you are not getting hours minutes and seconds..

    the
    $date = strtotime("+5 days");
    PHP:
    gives you a timestamp with hours, minutes and seconds.. looks somthing like thsi
    1330949208

    while the
    $udate = mktime(0, 0, 0, $m, $d, $y);
    PHP:
    gives you something like this
    1330902000

    Since you are not working with hours, mins and seconds I suggest you deduct them from the $date.... I don't know how to do that, since I don't work much with dates, but I bet you can make it :p


     
    Last edited: Feb 29, 2012
    GMF, Feb 29, 2012 IP
  4. afridy

    afridy Well-Known Member

    Messages:
    810
    Likes Received:
    116
    Best Answers:
    0
    Trophy Points:
    135
    #4
    thanks buddy, yes ur correct :D
    I think i made it now in my post #2
     
    afridy, Feb 29, 2012 IP