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

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

Postby markl999 » Tue Oct 21, 2003 11:17 am

To check a date is entered in YYYY-MM-DD format.
Syntax: [ Download ] [ Hide ]
 
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.
User avatar
markl999
DevNet Resident
 
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Postby jason » Tue Oct 21, 2003 11:32 am

A little extension to this.

Syntax: [ Download ] [ Hide ]
$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.
jason
Site Admin
 
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA

Postby evilMind » Tue Oct 21, 2003 12:16 pm

alternative, with validation:
Syntax: [ Download ] [ Hide ]
 
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/viewtopic.php?t=13792&start=0&postdays=0&postorder=asc&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.
evilMind
Forum Contributor
 
Posts: 145
Joined: Fri Sep 19, 2003 10:09 am
Location: Earth

Postby jason » Tue Oct 21, 2003 12:27 pm

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:

Syntax: [ Download ] [ Hide ]
 
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.
jason
Site Admin
 
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA

Postby fastfingertips » Sat Jan 03, 2004 3:35 am

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().
fastfingertips
Forum Contributor
 
Posts: 242
Joined: Sun Dec 28, 2003 2:40 am

Postby redmonkey » Sun Jan 04, 2004 4:36 pm

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:

Syntax: [ Download ] [ Hide ]
 
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.

Syntax: [ Download ] [ Hide ]
 
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.
redmonkey
Forum Regular
 
Posts: 836
Joined: Thu Dec 18, 2003 4:58 pm

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

Postby volomike » Fri Jun 27, 2008 6:47 pm

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

Syntax: [ Download ] [ Hide ]
 
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]);
}
 
User avatar
volomike
Forum Regular
 
Posts: 632
Joined: Wed Jan 16, 2008 10:04 am
Location: Myrtle Beach, South Carolina, USA

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

Postby matthijs » Thu Jul 03, 2008 2:26 am

What is $xadBits and were is it coming from? And what does checkdate look like?
matthijs
DevNet Master
 
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

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

Postby volomike » Thu Jul 03, 2008 3:20 am

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.
User avatar
volomike
Forum Regular
 
Posts: 632
Joined: Wed Jan 16, 2008 10:04 am
Location: Myrtle Beach, South Carolina, USA

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

Postby syntia19 » Mon Sep 19, 2011 3:00 am

Hi,
thank you, exactly what I'm looking for!
syntia19
Forum Newbie
 
Posts: 1
Joined: Mon Sep 19, 2011 2:58 am

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

Postby Shirlee » Mon Jan 09, 2012 10:43 am

Thank you very much!
Shirlee
Forum Newbie
 
Posts: 1
Joined: Mon Jan 09, 2012 10:23 am


Return to Code Snippets

Who is online

Users browsing this forum: No registered users and 3 guests