Last active
October 1, 2015 09:27
-
-
Save smddzcy/328cb8f788ab6f574d16 to your computer and use it in GitHub Desktop.
One line regExp date builder for last 30 days in PHP. Doesn't match dates with ':' delimiter; since they are obviously "hours", not "dates" ._.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$regexpLastThirty = '#' . '(' . | |
/* | |
* regExp builder for this month | |
* d-m-Y m-d-Y Y-d-m Y-m-d | |
* d-m-y m-d-y y-d-m y-m-d | |
*/ | |
implode('|', array_map(function ($v) { | |
return | |
'(' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad(date('m'), 2, 0, STR_PAD_LEFT) . '[^:]?' . date('Y') . ')' | |
. '|' . '(' . str_pad(date('m'), 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . date('Y') . ')' | |
. '|' . '(' . date('Y') . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad(date('m'), 2, 0, STR_PAD_LEFT) . ')' | |
. '|' . '(' . date('Y') . '[^:]?' . str_pad(date('m'), 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . ')' | |
. '|' . '(' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad(date('m'), 2, 0, STR_PAD_LEFT) . '[^:]?' . substr(date('Y'), -2) . ')' | |
. '|' . '(' . str_pad(date('m'), 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . substr(date('Y'), -2) . ')' | |
. '|' . '(' . substr(date('Y'), -2) . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad(date('m'), 2, 0, STR_PAD_LEFT) . ')' | |
. '|' . '(' . substr(date('Y'), -2) . '[^:]?' . str_pad(date('m'), 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . ')'; | |
}, range(01, date('d')))) . '|' . | |
/* | |
* regExp builder for previous month | |
* d-m-Y m-d-Y Y-d-m Y-m-d | |
* d-m-y m-d-y y-d-m y-m-d | |
*/ | |
implode('|', array_map(function ($v) { | |
if (date('m') != '01') { | |
return | |
'(' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad((date('m') - 1), 2, 0, STR_PAD_LEFT) . '[^:]?' . date('Y') . ')' | |
. '|' . '(' . str_pad((date('m') - 1), 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . date('Y') . ')' | |
. '|' . '(' . date('Y') . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad((date('m') - 1), 2, 0, STR_PAD_LEFT) . ')' | |
. '|' . '(' . date('Y') . '[^:]?' . str_pad((date('m') - 1), 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . ')' | |
. '|' . '(' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad((date('m') - 1), 2, 0, STR_PAD_LEFT) . '[^:]?' . substr(date('Y'), -2) . ')' | |
. '|' . '(' . str_pad((date('m') - 1), 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . substr(date('Y'), -2) . ')' | |
. '|' . '(' . substr(date('Y'), -2) . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad((date('m') - 1), 2, 0, STR_PAD_LEFT) . ')' | |
. '|' . '(' . substr(date('Y'), -2) . '[^:]?' . str_pad((date('m') - 1), 2, 0, STR_PAD_LEFT) . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . ')'; | |
} else { | |
return | |
'(' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . '12' . '[^:]?' . (date('Y') - 1) . ')' | |
. '|' . '(' . '12' . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . (date('Y') - 1) . ')' | |
. '|' . '(' . (date('Y') - 1) . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . '12' . ')' | |
. '|' . '(' . (date('Y') - 1) . '[^:]?' . '12' . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . ')' | |
. '|' . '(' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . '12' . '[^:]?' . substr((date('Y') - 1), -2) . ')' | |
. '|' . '(' . '12' . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . substr((date('Y') - 1), -2) . ')' | |
. '|' . '(' . substr((date('Y') - 1), -2) . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . '[^:]?' . '12' . ')' | |
. '|' . '(' . substr((date('Y') - 1), -2) . '[^:]?' . '12' . '[^:]?' . str_pad($v, 2, 0, STR_PAD_LEFT) . ')'; | |
} | |
}, range(date('d'), 31))) | |
. ')' . '#si'; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment