1

I want to use PHP's filter_input with FILTER_VALIDATE_INT to validate time in hours and minutes apart. the problem is that this function returns false when the time is like 03:07

$hours_range = array (
'options' => array (
    'min_range' => 0,
    'max_range' => 23
)
);

$minutes_range = array (
'options' => array (
    'min_range' => 0,
    'max_range' => 59
)
);

filter_input(INPUT_POST, 'hour', FILTER_VALIDATE_INT, $hours_range)

filter_input(INPUT_POST, 'minute', FILTER_VALIDATE_INT, $minutes_range)
3
  • Isn't it the colon that is causing trouble? Commented Apr 18, 2014 at 22:03
  • I usually get the DateTime class to do this sort of validation as part of a callback filter. Commented Apr 18, 2014 at 22:08
  • Nm, missed something in your question. Commented Apr 19, 2014 at 8:54

1 Answer 1

4

Hmm... You could try another filter here - I would probably try FILTER_VALIDATE_REGEXP. Maybe something like:

$mins = filter_input(INPUT_POST, 'hour', FILTER_VALIDATE_REGEXP, ['options' => [
    'regexp' => '/\d{1,2}/']
]);

Just adding the regex \d{1,2} as an example here, clearly you might want to define something better here. Alternatively you could use FILTER_CALLBACK and define a callback inside which you can check if your value is an int and explicitly within your required numeric range.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice touch with regexp.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.