3

Does anybody know why this function, when passed an invalid date (e.g. timestamp) to it, still throws an error despite the try-catch?

function getAge($date){
    try {
        $dobObject = new DateTime($date);
        $nowObject = new DateTime();

        $diff = $dobObject->diff($nowObject);
    }

    catch (Exception $e) {
        echo 'Error: ',  $e->getMessage();
    }

    return $diff->y;
}

Error:

Fatal error: Uncaught exception 'Exception' with message 'DateTime::_construct() [datetime.--construct]: Failed to parse time string (422926860) at position 7 (6): Unexpected character' in ... .php:4 Stack trace: #0 ... .php(4): DateTime->_construct('422926860') #1 ... .php(424): getAge('422926860') #2 {main} thrown in/... .php on line 4

Thank you very much in advance!

1
  • I tried your code and it works perfectly. The exception was catched and the 'Error: …' was displayed without a fatal error. Your error must reside either somewhere else in your code (which one is line four?) or in your PHP .ini-configuration or version. I was using PHP 5.4.0 @Ubuntu 12.04. Commented Jul 5, 2012 at 12:25

1 Answer 1

6

Chris, you cannot catch fatal errors, at very least you shouldn't.

Quoting keparo:

PHP won't provide you with any conventional means for catching fatal errors because they really shouldn't be caught. That is to say, you should not attempt to recover from a fatal error. String matching an output buffer is definitely ill-advised.

If you simply have no other way, take a look at this post for more info and possible how-tos.

Try this:

function isDateValid($str) {

  if (!is_string($str)) {
     return false;
  }

  $stamp = strtotime($str); 

  if (!is_numeric($stamp)) {
     return false; 
  }

  if ( checkdate(date('m', $stamp), date('d', $stamp), date('Y', $stamp)) ) { 
     return true; 
  } 
  return false; 
} 

And then :

 if isDateValid( $yourString ) {
    $date = new DateTime($yourString);
 }
Sign up to request clarification or add additional context in comments.

5 Comments

Thx, Nemanja. Yeah, you may be right. Just, I am trying to display users on a page and want to print their age. If the function breaks because of a wrong input, I don't think the whole application should break either. So, you are right, it is a fatal error, but I'd rather like the page displayed without the age than a fatal error... what do you think? Is this a reason for the whole page to break?
Nope, i think fatal exception in this case is a manifestation of PHP's stupid nature. :) But alas, you have to adapt. I'll scribble a workaround for you and edit my question.
hey, you don't have to scribble sth, really! what about just using checkdate() to verify the correct date?
Already did, checkout my answer - i edited it. Needed a little bit more than just check date, but yes, basically that is IMHO the correct way of preventing the exception.
So if I use DateTime's __construct() attempting to parse 'next frday'//accidental typo my application will crash on account of the fatal? Thanks, Obama.

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.