A few words of thanks would be greatly appreciated.

Verifying date format in PHP

PHP is widely used these days by many people. Perhaps this even leads to the resulting queries from new users, such as the validation of PHP date format. PHP does not use any complex syntax/expressions for verification of date format, rather the standard DD/MM/YYYY expression can be used. This date format syntax in PHP must have the date and month in single digits only. Below is a simple illustration which shows general verification of the PHP date format. This program is used to verify the input digits given by a user on any PHP platform. One can only verify the date format and not the validity of the date.

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

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

 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:
This function does not check the validity of the date itself, but only the validity of its format.

See also:

Useful links for PHP

A few words of thanks would be greatly appreciated.

Ask a question
CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jean-François Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.
This document, titled « Verifying date format in PHP », is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).