PHP DateTime exception and errors handling

Check out the documentation on DateTime(), here's a little snippet:

<?php
try {
    $date = new DateTime('2000-01-01');
} catch (Exception $e) {
    echo $e->getMessage();
    exit(1);
}

echo $date->format('Y-m-d');
?>

PHP Manual DateTime::__construct()


What about exception handling?

try {
    $in = new DateTime($in);
} catch (Exception $e) {
    echo $e->getMessage();
    return(0);
}

strtotime() will return false if the format is bad so this should catch bad formats.

if (strtotime($in) === false)
{
     // bad format
}

Tags:

Datetime

Php