I'm writing a PHP script that takes in a user entered value that MUST be something like this.
2011-06-17 00:00:00
...
2011-06-17 23:59:59
How do I verify that it is indeed a correct input?
I'm writing a PHP script that takes in a user entered value that MUST be something like this.
2011-06-17 00:00:00
...
2011-06-17 23:59:59
How do I verify that it is indeed a correct input?
Alternatively, use strtotime() on the input, and then date() to put it in your required format. This has the advantage of validating that the user provided a correct date, not just a correct format. I.e., a regex check won't catch when somebody puts in Feb 31.
$date = strtotime($input);
if ($date === false) {
throw Exception('bad date');
}
$formatted = date('<whatever>', $date);