Simple date regexp problem

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
ironosity
Forum Newbie
Posts: 3
Joined: Tue Oct 02, 2007 4:31 pm

Simple date regexp problem

Post by ironosity »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


So here's my php function:

Code: Select all

function valDate($date)
{
   $valid = true;
   echo "In Date = ".$date."<br />";
   //if (!eregi("([1-9]|(0[1-9])|([1-2][0-9])|3[0-1])\/((0[1-9])|(1[0-2]))\/[0-9]{1,4}$", $date))
   if(!eregi("\d{2}\/\d{2}\/\d{4}", $date))
   {
      $valid = false;
      echo "valid after = ".$valid."<br/>";
   }   
   return $valid;
}
The problem is that this function always returns false. I only want it to return false if a date is NOT found. I know it must be something simple. Any help would be much appreciated. Also note that I commented out the line I wanted to use and used a less complicated regex so I could debug. Thanks again.

Ironosity[/b]


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Marcel83
Forum Newbie
Posts: 5
Joined: Mon Oct 01, 2007 12:07 pm

Post by Marcel83 »

Hello,

At first: As far as i know, eregi is deprecated.
So use preg_match.

Test this:


Code: Select all

//$testvar = '01.01.2007';
//$testvar = '01-01-2007';
$testvar = '01/01/2007';

preg_match('(/\d{1,2}(\//|.|-)){2}(\d{2}|\d{4})/',$testvar);
ironosity
Forum Newbie
Posts: 3
Joined: Tue Oct 02, 2007 4:31 pm

Still doesn't work

Post by ironosity »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Okay, so I tried your code modifying my function to be:

Code: Select all

function valDate($postedDate)
{
   $testvar = '01/01/2007';
   return preg_match('(/\d{1,2}(\//|.|-)){2}(\d{2}|\d{4})/',$testvar); 
}
Which is called with:

Code: Select all

if (!valDate($burialDeathDate))
   {
      echo "got to burialDeathDate<br/>";
      $isValid = false;
   }
So if valDate finds the pattern it returns true which is negated to false preventing it from entereing the conditional. I don't know why it's not working. any ideas?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
ironosity
Forum Newbie
Posts: 3
Joined: Tue Oct 02, 2007 4:31 pm

My Solution...

Post by ironosity »

Thanks for the help. Got it working with much trial and error and looking through other patterns (and the help of a friend) and here's my solution:

Code: Select all

function valDate($postedDate)
{
   if(preg_match('/(\d{4})((-)\d{1,2}){2}/', $postedDate, $matches))
   {
      list( $year , $month , $day ) = explode('-',$postedDate);
      return( checkdate( $month , $day , $year ) );
   }
   else
   {
      return false;//$matches;
   }
}
I'm just posting in case. anyhow thanks!
User avatar
GeertDD
Forum Contributor
Posts: 274
Joined: Sun Oct 22, 2006 1:47 am
Location: Belgium

Post by GeertDD »

Since you're not using $matches in your code, you could just leave it out and call preg_match() with just two parameters, which is faster. However maybe you could use them and drop the explode() part.

Code: Select all

if(preg_match('/(\d{4})-(\d{1,2})-(\d{1,2})/', $postedDate, $matches))
   {
      return checkdate($matches[2] , $matches[3], $matches[1]);
   }
Post Reply