Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2006
    Posts
    36

    How to validate between date in PHP

    I tried followng query, but it's giving wrong result.


    if( $start_date > $current_date && $expiry_date < $current_date)
    {
    }

    thanks
    Raji

  2. #2
    Join Date
    Feb 2005
    Posts
    105
    Use parentheses.

    if(($start_date > $current_date) && ($expiry_date < $current_date))
    {
    }

    I believe due to order of precedence, your code is first calculating
    $start_date > $current_date
    and then taking that result and && $expiry_date

    But you want to calculate the left side first, then the right... then do a logical &&.

  3. #3
    Join Date
    Sep 2005
    Location
    Southern California
    Posts
    179
    Quote Originally Posted by MrMan
    I believe due to order of precedence, your code is first calculating
    $start_date > $current_date
    and then taking that result and && $expiry_date

    But you want to calculate the left side first, then the right... then do a logical &&.

    I doubt that would help, as dates are usually strings. Assuming your dates are actually strings (such as "2006-12-02") the easiest way would be:

    PHP Code:
    if ( strtotime($start_date) > strtotime($current_date) && strtotime($expiry_date) < strtotime($current_date) )
    {
    //


  4. #4
    Join Date
    Feb 2005
    Location
    Australia
    Posts
    5,842
    Actually the whole point of 'Y-m-d' date format is that string comparisons do work. Chances are in this case one or more of the dates being compared is formatted differently, eg. "Sun, 2 Dec 2006".

    strtotime() should still work but it would perhaps be safer to get the dates in a consistent format in the first place.
    Chris

    "Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them." - Laurence J. Peter

  5. #5
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    If you are fetching these from a database, then use the date+time functions to return properly formatted dates (timestamps would be best).
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •