Regex: Check date is in YYYY-MM-DD format

Small, short code snippets that other people may find useful. Do you have a good regex that you would like to share? Share it! Even better, the code can be commented on, and improved.

Moderator: General Moderators

Post Reply
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Regex: Check date is in YYYY-MM-DD format

Post by markl999 »

To check a date is entered in YYYY-MM-DD format.

Code: Select all

 
if(preg_match('/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/', $datein)){ 
    //it's ok 
}else{ 
    //it's bad 
}
 
This only checks the format and not the validity. E.g. 2003-09-31 will pass the check even though September doesn't have 31 days.
Last edited by feyd on Fri Aug 01, 2008 10:04 am, edited 1 time in total.
Reason: fix php tags.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

A little extension to this.

Code: Select all

$datein = '1998-09-21';
if(preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $datein)){
    echo 'go';
}else{
    echo 'no go';
}
Makes sure things like 1998-13-32 won't get past and validate.
Last edited by feyd on Fri Aug 01, 2008 10:04 am, edited 1 time in total.
Reason: fix php tags.
evilMind
Forum Contributor
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Post by evilMind »

alternative, with validation:

Code: Select all

 
function MyCheckDate( $postedDate ) {
   if ( ereg("^[0-9]{4}-[01][0-9]-[0-3][0-9]$",$postedDate) ) {
      list( $year , $month , $day ) = explode('-',$postedDate);
      return( checkdate( $month , $day , $year ) );
   } else {
      return( false );
   }
}
 
Check http://www.devnetwork.net/forums/viewto ... highlight=
for a breakdown of the ereg
Last edited by feyd on Fri Aug 01, 2008 10:03 am, edited 1 time in total.
Reason: fix php tags.
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Of course, anyone can simply replace the pregular expression from above with the ereg and get the same result with the function resulting in this:

Code: Select all

 
function MyCheckDate( $postedDate ) {
   if ( preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $postedDate) ) {
      list($year , $month , $day) = explode('-',$postedDate);
      return checkdate($month , $day , $year);
   } else {
      return false;
   }
} 
 
Last edited by feyd on Fri Aug 01, 2008 10:03 am, edited 1 time in total.
Reason: fix php tags.
fastfingertips
Forum Contributor
Posts: 242
Joined: Sun Dec 28, 2003 1:40 am
Contact:

Post by fastfingertips »

I'm using preg_match simply because faster then ereg.
Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg().
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

jason wrote:Of course, anyone can simply replace the pregular expression from above with the ereg and get the same result with the function resulting in this:

Code: Select all

 
function MyCheckDate( $postedDate ) {
   if ( preg_match('/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/', $postedDate) ) {
      list($year , $month , $day) = explode('-',$postedDate);
      return checkdate($month , $day , $year);
   } else {
      return false;
   }
} 
 
Of course, the function could be simplified by removing the list/explode functions from the function resulting in this.

Code: Select all

 
function MyCheckDate( $postedDate ) {
   if (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $postedDate, $datebit)) {
      return checkdate($datebit[2] , $datebit[3] , $datebit[1]);
   } else {
      return false;
   }
} 
 
As the date validity is further checked against checkdate() then only simple pattern matching is required.
Last edited by feyd on Fri Aug 01, 2008 10:03 am, edited 1 time in total.
Reason: fix php tags.
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Regex: Check date is in YYYY-MM-DD format

Post by volomike »

My version below checks if date is in MM/DD/YYYY format or MM-DD-YYYY format or MM DD YYYY format.

Code: Select all

 
public static function CheckValidDate($sDate) {
    $sDate = str_replace(' ', '-', $sDate);
    $sDate = str_replace('/', '-', $sDate);
    $sDate = str_replace('--', '-', $sDate);
    preg_match('/^(\d{2})-(\d{2})-(\d{4})$/', $sDate, $xadBits);
    return checkdate($xadBits[1], $xadBits[2], $xadBits[3]);
}
 
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: Regex: Check date is in YYYY-MM-DD format

Post by matthijs »

What is $xadBits and were is it coming from? And what does checkdate look like?
User avatar
volomike
Forum Regular
Posts: 633
Joined: Wed Jan 16, 2008 9:04 am
Location: Myrtle Beach, South Carolina, USA

Re: Regex: Check date is in YYYY-MM-DD format

Post by volomike »

matthijs wrote:What is $xadBits and were is it coming from? And what does checkdate look like?
$x- prefix means "byref", meaning I'm passing or receiving a value that will get updated without being returned as a result of a function.
-a- prefix means array
-d- prefix means date

So this translates to, "byref array of dates, broke up into bits". This is my Adapted Hungarian Notation because Hungarians are cool.

The PHP function returns that value to us -- we don't give it an initial value.
syntia19
Forum Newbie
Posts: 1
Joined: Mon Sep 19, 2011 2:58 am

Re: Regex: Check date is in YYYY-MM-DD format

Post by syntia19 »

Hi,
thank you, exactly what I'm looking for!
Shirlee
Forum Newbie
Posts: 1
Joined: Mon Jan 09, 2012 9:23 am

Re: Regex: Check date is in YYYY-MM-DD format

Post by Shirlee »

Thank you very much!
TammaraBb1
Forum Newbie
Posts: 2
Joined: Wed Oct 15, 2014 1:49 am

Re: Regex: Check date is in YYYY-MM-DD format

Post by TammaraBb1 »

Take note of the format guys. Most of us missed it.
Post Reply