How can I prevent PHP to crash when creating a DateTime object?
$in = new DateTime($in);
$out = new DateTime($out);
$in
and $out
both comes from a form so they could be anything. I enforce the user to use a calendar and block it to dates with javascript. What if the user can bypass this check?
If $in = "anything else other than a date"
PHP will crash and block the rendering of the whole page.
How do I prevent this and just return(0)
if PHP is not able to parse the date?
Check out the documentation on
DateTime()
, here's a little snippet:PHP Manual DateTime::__construct()