Jump to content

[SOLVED] validate date against date format


zohab

Recommended Posts

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
Share on other sites

'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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.