note 107569 deleted from function.checkdate by googleguy

From: Date: Fri, 11 Jan 2013 09:26:11 +0000
Subject: note 107569 deleted from function.checkdate by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-192908@lists.php.net to get a copy of this message
Note Submitter: pmmmm ---- Here's a function that further builds on top of what was posted earlier (bmauser and ystein M). It takes date and format (D, DD, M, MM, YY, YYYY) and any seperator. Format example: YYYY-MM-DD. The previous functions didn't take dashes and required specific order. <?php function valid_date($date, $format = 'YYYY-MM-DD'){ if(strlen($date) >= 8 && strlen($date) <= 10){ $separator_only = str_replace(array('M','D','Y'),'', $format); $separator = $separator_only[0]; if($separator){ $regexp = str_replace($separator, "\\" . $separator, $format); $regexp = str_replace('MM', '(0[1-9]|1[0-2])', $regexp); $regexp = str_replace('M', '(0?[1-9]|1[0-2])', $regexp); $regexp = str_replace('DD', '(0[1-9]|[1-2][0-9]|3[0-1])', $regexp); $regexp = str_replace('D', '(0?[1-9]|[1-2][0-9]|3[0-1])', $regexp); $regexp = str_replace('YYYY', '\d{4}', $regexp); $regexp = str_replace('YY', '\d{2}', $regexp); if($regexp != $date && preg_match('/'.$regexp.'$/', $date)){ foreach (array_combine(explode($separator,$format), explode($separator,$date)) as $key=>$value) { if ($key == 'YY') $year = '20'.$value; if ($key == 'YYYY') $year = $value; if ($key[0] == 'M') $month = $value; if ($key[0] == 'D') $day = $value; } if (checkdate($month,$day,$year)) return true; } } } return false; } ?>

« previous php.notes (#192908) next »