I am looking for a versatile method for validating dates.
What I want, is some way of achieving validation rules such as:
First monday of month
monday or friday
Fourth day of month
Is there anything that can handle such requirements? Or does anyone have any pointers as to how i could achieve this?
Edit: I appreciate the answers, but heres what i ended up using for now:
Thanks for all the answers. What i actually ended up using (At least till i find the perfect solution) is roughly this function:
function isDateValid($rule,$date){
$DATE = (int) $date;
/* Make it safe for eval */
preg_match_all('/:\w:|&&|\|\||<|=|>|!|\(|\)|\d+/i',$rule,$filteredRule);
$rule = implode('',$filteredRule[0]);
//replace rule notations
$f = function($str){
return "date('$str[1]',\$DATE)";
};
//Or if php < 5.3: $f = create_function('$str', 'return "date(\'$str[1]\',\$DATE)";');
$rule = preg_replace_callback('/:(\w):/i', $f, $rule);
//Evaluate expression
return @eval("return $rule;");
}
//Example of usage testing first of month:
var_dump( isDateValid(':d:==1',time()) );
This lets me handle all the requirements i have in, well PHP code. The people who use this is very limited (<5 persons) and most of them have some degree of php experience. Now i can make rules such as:
first monday of month
: :w:==1 && :d: <= 7
monday or friday
: :w:==1 || :w:==5
Fourth day of month
: :d: == 4
I would appreciate an even better more flexible solution or feedback regarding security (Can this be injected with mallicious php code?)