Jump to content

regex function to match a date in yyyy-mm-dd format accounting for length of month


terungwa

Recommended Posts

I have set up a function to validate user input date in yyyy-mm-dd format taking into account the following possibilities:

  1. the length of the month
  2. 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);

php has a function called checkdate(), which means your function definition isn't being made and your code is likely throwing php errors when you call php's built-in checkdate() function with those parameters.

 

you need to ALWAYS have php's error_reporting set to E_ALL and have display_errors set to ON, when developing code, to get php to help you.

Besides that, the implementation is horrible. Sorry.

 

Regexes are for patterns. They're not suitable for range checks. That's what we have the mathematical operators “<”, “<=”, “>” and “>=” for.

 

It's also insane to do date calculations by hand. Even if you actually manage to find a PHP version which doesn't have checkdate(), the basic date functions are always available. I mean, handling dates is one of the core features of every language. And, yes, PHP can do it as well.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.