PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sun Nov 23, 2014 9:25 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
PostPosted: Tue Oct 21, 2003 11:17 am 
Offline
DevNet Resident
User avatar

Joined: Thu Oct 16, 2003 5:49 pm
Posts: 1972
Location: Manchester (UK)
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.
fix php tags.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 11:32 am 
Offline
Site Admin

Joined: Thu Apr 18, 2002 3:14 pm
Posts: 1767
Location: Montreal, CA
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.
fix php tags.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 12:16 pm 
Offline
Forum Contributor

Joined: Fri Sep 19, 2003 10:09 am
Posts: 145
Location: Earth
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.
fix php tags.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 21, 2003 12:27 pm 
Offline
Site Admin

Joined: Thu Apr 18, 2002 3:14 pm
Posts: 1767
Location: Montreal, CA
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.
fix php tags.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 03, 2004 3:35 am 
Offline
Forum Contributor

Joined: Sun Dec 28, 2003 2:40 am
Posts: 242
I'm using preg_match simply because faster then ereg.

Quote:
Note: preg_match(), which uses a Perl-compatible regular expression syntax, is often a faster alternative to ereg().


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 04, 2004 4:36 pm 
Offline
Forum Regular

Joined: Thu Dec 18, 2003 4:58 pm
Posts: 836
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.
fix php tags.


Top
 Profile  
 
PostPosted: Fri Jun 27, 2008 6:47 pm 
Offline
Forum Regular
User avatar

Joined: Wed Jan 16, 2008 10:04 am
Posts: 632
Location: Myrtle Beach, South Carolina, USA
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]);
}
 


Top
 Profile  
 
PostPosted: Thu Jul 03, 2008 2:26 am 
Offline
DevNet Master

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


Top
 Profile  
 
PostPosted: Thu Jul 03, 2008 3:20 am 
Offline
Forum Regular
User avatar

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


Top
 Profile  
 
PostPosted: Mon Sep 19, 2011 3:00 am 
Offline
Forum Newbie

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


Top
 Profile  
 
PostPosted: Mon Jan 09, 2012 10:43 am 
Offline
Forum Newbie

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


Top
 Profile  
 
PostPosted: Wed Oct 15, 2014 8:55 am 
Offline
Forum Newbie

Joined: Wed Oct 15, 2014 1:49 am
Posts: 2
Take note of the format guys. Most of us missed it.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 12 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group