#1
  1. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,480
    Rep Power
    221

    Easiest way to validate mysql date format in php?


    Hi;

    how come there are a million different solutions for is_valid_mysql_date and none of them are the same?
    PHP Code:
    if(is_valid_mysql_date($date))
    {
      
    //return true if format is YYYY-MM-DD

    What's the best way to do this painlessly?

    If I wanna use "checkdate " then I have to explode($query->result_array()[0]['date_added']);

    If it's not a string then I get another error on exploding.

    PHP Code:
    $date explode("-"$query->result_array()[0]['date_added']);
                            if(isset(
    $date[1]) && isset($date[2]) && isset($date[0]) )
                            {
                                if(
    checkdate($date[1], $date[2], $date[0]))
                                  {
                                    
    $datediff time() - strtotime($query->result_array()[0]['date_added']);
                                    return 
    floor($datediff / (60 60 24));
                                  }
                                else 
                                  {
                                    echo 
    "Invalid launch add date";exit();
                                  }
                            }
                            else 
                              {
                                echo 
    "Invalid launch add date!";exit();
                              } 
    I wanna quick fix for this one.
    Last edited by English Breakfast Tea; April 8th, 2018 at 11:28 AM.
  2. #2
  3. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,252
    Rep Power
    4150
    Why do you need to validate it? If you're querying a date column mysql will give you back a valid date (or null), not gibberish.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2016
    Location
    Cheshire, UK
    Posts
    86
    Rep Power
    72
    Again, use mysql datetime functions
    Code:
    SELECT DATEDIFF(CURDATE(), date_added) as daysdiff
  6. #4
  7. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,480
    Rep Power
    221
    Originally Posted by kicken
    Why do you need to validate it? If you're querying a date column mysql will give you back a valid date (or null), not gibberish.
    I am pretty sure it will calculate wrong results

    PHP Code:
    $datediff time() - strtotime($query->result_array()[0]['date_added']); 
  8. #5
  9. A Change of Season
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,480
    Rep Power
    221
    Originally Posted by Barand
    Again, use mysql datetime functions
    Code:
    SELECT DATEDIFF(CURDATE(), date_added) as daysdiff
    Wtf! Where were you 3 days ago? This is dope

IMN logo majestic logo threadwatch logo seochat tools logo