Page 1 of 1

Creating RegEx pattern from date range

Posted: Mon Jan 17, 2011 1:00 pm
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.

Re: Creating RegEx pattern from date range

Posted: Mon Jan 17, 2011 1:52 pm
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.