Jump to content

is_date or is_timestamp


andyd34

Recommended Posts

You need to make use of the preg_match function using an expression.

'/^[0-2]{2}\/[0-9]{2}\/[0-9]{4}$/'

means the following:

/^ = from the start of the string

[0-9]{2} = look for 2 consecutive numbers between 0 and 9 (for the months)

\/ = look for a "/"

[0-9]{2} = look for 2 consecutive numbers between 0 and 9 (for the days)

\/ = look for a "/"

[0-9]{4} = look for 4 consecutive numbers between 0 and 9 (for the years)

$/ = till the end of the string

 

Beware however that this will validate FORMAT ONLY. i.e 99/99/9999 will pass as a valid format, but its not a valid date.

 

<?php
$dateFormatToValidate = "01/01/2010";
if(preg_match('/^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/', $dateFormatToValidate)){ 
    // is valid format, but might not necessarily be a valid date do further checking
    // ie. could be the 30th Feb.....
}else{ 
    // not valid 
}
?>

 

 

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.