Re: Checking a date for validity

From: Date: Thu, 08 Sep 2005 02:01:32 +0000
Subject: Re: Checking a date for validity
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-222005@lists.php.net to get a copy of this message
I would use something like:- $date = '09/09/2005'; list($month, $day, $year) = explode("/", $date); if(checkdate($month, $day, $year)) { /* valid date */ } else { /* invalid date */ } Todd Cary wrote:
I need to check the input of a user to make sure the date is valid and correctly formatted. Are there any examples available? Here is one solution I created: /* Is date good */ function is_date_good($date) {
    if (strtotime($date) == -1) {
      $retval = 0;
    } else {
      if (strpos($date, "/") > 0) {
        $parts = explode("/", $date);
      } elseif (strpos($date, "-") > 0) {
        $parts2 = explode("-", $date);
        $parts[0] = $parts2[1];
        $parts[1] = $parts2[2];
        $parts[2] = $parts2[0];
      } else {
        $parts = explode(".", $date);
      }
      //print_r($parts);
      if (checkdate($parts[0], $parts[1], $parts[2]) )
        return 1;
      else
        return 0;
    }
    return $retval;
} Is there a simplier solution? Many thanks......


« previous php.general (#222005) next »