Jump to content

Check valid date


yklxmas

Recommended Posts

Hello all,I'm trying to validate a given date.I tried this function$day = 02;$month = 02;$year = 2007;$DateValidation = mcal_date_valid($day, $month, $year);echo $DateValidation;but I was thrown an error saying undefine function. Doesn't this function exist in php5 anymore? If not, what would you advice to use?Many thanks in advance

Link to comment
Share on other sites

Hi!Try this function (I don't know of any predfined functions that can do that...):

function ValiDate( $date, $month = 0, $year = 0){	// If $date is a string	if (is_string( $date )) {		// Let's use preg_match fot date-strings (This will check for date format as day-month-full year)		//  It's not the best reg.exp... :?)		return preg_match( '/^[0-3][0-9]-[01][0-9]-[12][09][0-9][0-9]$/', $date );	} else {		// Get the length of the month		switch( $month ) {			case 1: case 3: case 5: case 7: case 8: case 10: case 12: {				$month_length = 31;				break;			 }			case 2: {				// Don't check for "leap years" right now...				$month_length = 29;				break;			 }			default: {				$month_length = 30;			 }		};		// Check date (integers...)		if (($date >= 1) && ($date <= $month_length) &&			($month >= 1) && ($month <= 12) &&			($year >= 1931) && ($year <= 2030))				return true;		else				return false;	}}

Usage :

$valid = valiDate( $day, $month, $year );or$valid = valiDate( '02-03-2004' );

Hope that helped some.. :?)Good Luck and Don't Panic!

Link to comment
Share on other sites

http://www.php.net/manual/en/function.mcal-date-valid.phpAvailable as of PHP4. According to the ref for MCAL:http://www.php.net/manual/en/ref.mcal.php
Note: This extension has been moved to the ยป PECL repository and is no longer bundled with PHP as of PHP 5.0.0. Note: PHP had an ICAP extension previously, but the original library and the PHP extension is not supported anymore. The suggested replacement is MCAL. Note: This extension is not available on Windows platforms.
Link to comment
Share on other sites

Archived

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

×
×
  • Create New...