Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload