Verifying date format in PHP

Last update on August 15, 2009 11:18 AM by jak58
Published by netty5

Verifying date format in PHP








There are several ways to check the format of a date in PHP; the simplest method is to make use regular expressions.

To confirm a date format DD/MM/YYYY, where the days and/or month can be given as single number:

<?php
	function testDate( $value )
	{
		return preg_match( '`^\d{1,2}/\d{1,2}/\d{4}$`' , $value ) )
	}
	
	testDate( '21/11/1999' ); // -> true
	testDate( '3/9/2008' ); // -> true

	testDate( 'a/04/2003' ); // -> false
	testDate( '28-01-2000' ); // -> false

	testDate( '99/13/1978' ); // -> true
?>

Note that:
As the last call, this function does not check the validity of the date itself but only the validity of its format.

See also:

Useful links for PHP
Best answers for « Verifying date format in PHP » in :
Connecting to an Ingres database with PHP ShowConnecting to an Ingres database with PHP Getting started Download PECL Installation Windows Linux & Unix Getting connected This tip is based on following Ingres documentation: Getting started Ingres® 2006 Release 2 for...
Parsing a binary file in PHP ShowParsing a binary file in PHP When using low level languages like C or Pascal, it is a common procedure, to data in a binary file (a record that can't be translated into text). Using C language, suppose you want to save the value...
Formatting Your Hard Drive ShowFormatting Your Hard Drive Formatting and installing your Hard drive using the installation CD ] A hard disk drive is a non-volatile device mainly for data storage. Usually faster than any other storage device, this essential...
Download Damaged DOCX2TXT ShowDamaged DOCX2TXT is a program to recover corrupt Word files. Its use of Perl language allows you to extract the damaged text from Word 2007. Since Word 2007 uses XML files as data format, it is difficult to locate the damaged texts. To solve the...
Databases - Using forms ShowUsing forms In order to use databases, the user must be provided with an interface that allows him or her to view data based on certain criteria. There is a tool for this: forms. A form is an interface with components for displaying, entering, or...
HTML forms ShowForms Interactive forms let web page authors give their pages interactive elements, such as for receiving messages from their readers, much like the reply cards found in some magazines. The reader enters information by filling in fields or clicking...
S/PDIF format ShowS/PDIF format The S/PDIF standard ("Sony/Philips Digital Interface", or SPDIF for short) is a digital audio data transfer format. It is an international standard known as "IEC-958 type II", which defines both hardware specifications (physical...