PHP - Moved: Date Regex
This topic has been moved to PHP Regex.
http://www.phpfreaks.com/forums/index.php?topic=353870.0 Similar TutorialsHi, all! I've got a text field in my DB, and inside it i have several date formmated like "02/02/2001". I need to find, from all the found dates, which one is the newest. I thought using regex 'd be nice, am i right? Googlin, i've found http://www.roscripts.com/PHP_regular_expressions_examples-136.html, which lead me using something like: Code: [Select] function do_reg($text, $regex) { preg_match_all($regex, $text, $result, PREG_PATTERN_ORDER); for ($i = 0; $i < count($result[0]); $i++) { $result[0][$i]; } } But the given expressions are for cheking if there is any invalid date: Code: [Select] //Date yyyy-mm-dd //1900-01-01 through 2099-12-31 //Matches invalid dates such as February 31st '(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])' So, anyone? P.s.: this is because i'm importing a full-messed 'DB'. Thanks! I have set up a function to validate user input date in yyyy-mm-dd format taking into account the following possibilities:
the length of the month
February 29th when the year is not a leap year
When I run the code, I do not get any feedback whatsoever, when it should.
This is the code below and I would appreciate your thoughts:
if(!function_exists('checkdate')) { function checkdate($date, $checkyear, $currentmonth) { if($checkyear == 0)//if it is not a leap year { // if current month is february if($currentmonth == 2) { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|1[0-9]|2[0-8])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } // if current months are april, june, september or november elseif($currentmonth == 4 || $currentmonth == 6 || $currentmonth == 9 || $currentmonth ==11) { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|1[0-9]|2[0-8]|3[0])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } else { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-8]|3[01])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } } elseif($checkyear == 1)//if it is a leap year { // if current month is february if($currentmonth == 2) { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|1[0-9]|2[0-9])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } // if current months are april, june, september or november elseif($currentmonth == 4 || $currentmonth == 6 || $currentmonth == 9 || $currentmonth ==11) { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|1[0-9]|2[0-9]|3[0])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } else { if(!preg_match("/^(20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/", $date)) { // set error for date field return ($error = 'Invalid Date!'); } return ($error = 'valid Date!'); } } } $todate = getdate(); $currentmonth = $todate['mon']; $checkyear = date('L'); $date = 2014-02-31; echo checkdate($date, $checkyear, $currentmonth); Edited by terungwa, 26 July 2014 - 02:43 PM. This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=358535.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=344155.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=326775.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=317764.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=356497.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=356424.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=308051.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=347186.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=349116.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=320857.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=307000.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=350701.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=308636.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=350672.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=332077.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=320895.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=353729.0 This topic has been moved to PHP Regex. http://www.phpfreaks.com/forums/index.php?topic=352271.0 |