Creating RegEx pattern from date range

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

Moderator: General Moderators

Post Reply
zxarr
Forum Newbie
Posts: 1
Joined: Mon Jan 17, 2011 12:52 pm

Creating RegEx pattern from date range

Post by zxarr »

Good day,

I am new to PHP, but I thought I'd at least ask and see where this leads... I have a perl script that requires a date range to be entered as a regular expression. I cannot change this script.

For example:
2010(10(0[7-9]|1[0-9]|2[0-9]|3[0-1])|11(0[1-5])) gives me a date range of 20101007 - 20101105, which is the format YYYYMMDD.

I'm wondering where I start in taking a date range and converting it into a regular expression that can be passed onto this script. I can see how to do this if you're only replacing the year and month, but specific days can make the regex rather complicated.

Any thoughts? Ideas on where to start? Is this easy and I'm just not in the right mindset?


Thanks.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Creating RegEx pattern from date range

Post by AbraCadaver »

Shouldn't be that difficult except for calculating what acceptable days are within the range (February 30 and 31 are not valid).

It would make for a very long regex but it might be better to enumerate all dates between the two, join them with the | operator and send that entire string:

Code: Select all

$start = strtotime('20101007');
$stop = strtotime('20101105');

do {
    $dates[] = date('Ymd', $start);
} while( ($start = strtotime('+1 day', $start)) <= $stop);

$pattern = implode('|', $dates); 
Just thinking out loud.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply