Jump to content

Regex date w/ array


andremantovani

Recommended Posts

Hi, all!

 

I've got a text field in my DB, and inside it i have several date formmated like "02/02/2001". I need to find, from all the found dates, which one is the newest. I thought using regex 'd be nice, am i right?

Googlin, i've found http://www.roscripts.com/PHP_regular_expressions_examples-136.html, which lead me using something like:

 

function do_reg($text, $regex)
{
preg_match_all($regex, $text, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[0]); $i++) {
 $result[0][$i];
}
}

 

But the given expressions are for cheking if there is any invalid date:

 

//Date yyyy-mm-dd
//1900-01-01 through 2099-12-31
//Matches invalid dates such as February 31st
'(19|20)[0-9]{2}[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])'

 

So, anyone?

 

P.s.: this is because i'm importing a full-messed 'DB'.

 

 

Thanks!

Link to comment
Share on other sites

You really should have DB date columns structured as "date" or "datetime" or "timestamp".  This will solve these issues.  There is a reason DB's have date functions built into them.

 

So, If it were me, I would build a script, that would re-order the date, and insert it into a date field.  Making my future interactions with the DB, painless.

Link to comment
Share on other sites

Hey all!

 

Thanks for the replies; unfortunatelly, I think i was not so clear. Here's the deal: I'm organizing a messy DB, recreating all the structure and stuff. The mess comes in the text field, where are all the dates I need. For instance, it looks something like this:

modified: 12/12/2010. Text: blahblah; modified: 11/09/2009. Text: blahblah;

This text field doesn't have any pattern that allows me searching through it; and the last date that appears not always is the newest. So my thought is to first use a generic select; then, for each row returned, use regex to find all dates in the text field, and store them in a array. After, find from the array what's the newest date. And finally store it in the datetime column.

That's the steps... in fact all i need is the regex part, that i couldnt get to work.

 

Thanks!

Link to comment
Share on other sites

If you row look like this:

 

modified: 12/12/2010. Text: blahblah; modified: 11/09/2009. Text: blahblah;

 

Do you need both dates here and you'll compare them too?

 

Too capture them just use this regex:

 

'#\d{2}/\d{2}/\d{4}#'

 

You don't have to validate the dates they're probably correct.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.