zohab Posted July 16, 2009 Share Posted July 16, 2009 Hi , I have following dates and date format. 'Y-m-d' => '2009-07-16', 'm-d-Y' => '07-16-2009', 'd-m-Y' => '16-07-2009', 'Y/m/d' => '2009/07/16', 'm/d/Y' => '07/16/2009', 'd/m/Y' => '16/07/2009', 'Y.m.d' => '2009.07.16', 'd.m.Y' => '16.07.2009', 'm.d.Y' => '07.16.2009', I have following function which checks date against date format. function is_date($value, $format){ if(strlen($value) == 10 && strlen($format) == 10){ // find separator. Remove all other characters from $format $separator_only = str_replace(array('m','d','y'),'', $format); $separator = $separator_only[0]; // separator is first character if($separator && strlen($separator_only) == 2){ // make regex $regexp = str_replace('mm', '[0-1][0-9]', $value); $regexp = str_replace('dd', '[0-3][0-9]', $value); $regexp = str_replace('yyyy', '[0-9]{4}', $value); $regexp = str_replace($separator, "\\" . $separator, $value); if($regexp != $value && preg_match('/'.$regexp.'/', $value)){ // check date $day = substr($value,strpos($format, 'd'),2); $month = substr($value,strpos($format, 'm'),2); $year = substr($value,strpos($format, 'y'),4); if(@checkdate($month, $day, $year)) return true; } } } return false; } When i am using it as follow ,it is not giving result. if(!is_date("dd/mm/yy","2009-07-16"){ echo"errorxyz"; } if(!is_date("dd/mm/yy","07-16-2009"){ echo"errorxyz"; } if(!is_date("dd/mm/yy","2009.07.16"){ echo"errorxyz"; } if(!is_date("dd/mm/yy","16/07/2009"){ echo"errorxyz"; } any idea? PHP:5.2.6 MYSQL:5.0.51b Link to comment https://forums.phpfreaks.com/topic/166196-solved-validate-date-against-date-format/ Share on other sites More sharing options...
ignace Posted July 16, 2009 Share Posted July 16, 2009 'Y-m-d' => '2009-07-16' should be: 'Y-m-d' => '/[0-9]{4}-[0-9]{2}-[0-9]{2}/' Do this for all supported formats. function is_date($format, $date) { global $date_formats; if (!array_key_exists($format, $date_formats)) return false; // not a valid format return preg_match($date_formats[$format], $date); } Link to comment https://forums.phpfreaks.com/topic/166196-solved-validate-date-against-date-format/#findComment-876403 Share on other sites More sharing options...
zohab Posted July 16, 2009 Author Share Posted July 16, 2009 hi I need regular expression for other array keys than yyyy-mm-dd function is_date($date,$format) { $date_formats=array ( 'yyyy-mm-dd' => '/[0-9]{4}-[0-9]{2}-[0-9]{2}/', 'mm-dd-yyyy' => '?', 'dd-mm-yyyy' => '?', 'yyyy/mm/dd' => '?', 'mm/dd/yyyy' => '?', 'dd/mm/yyyy' => '?', 'yyyy.mm.dd' => '?', 'dd.mm.yyyy' => '?', 'mm.dd.yyyy' => '?', ); if (!array_key_exists($format, $date_formats)) return false; // not a valid format return preg_match($date_formats[$format], $date); } aby idea? Link to comment https://forums.phpfreaks.com/topic/166196-solved-validate-date-against-date-format/#findComment-876423 Share on other sites More sharing options...
zohab Posted July 17, 2009 Author Share Posted July 17, 2009 thanks Problem is solved Link to comment https://forums.phpfreaks.com/topic/166196-solved-validate-date-against-date-format/#findComment-876899 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.