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!

Chris
  • 3,756
  • 7
  • 35
  • 54
  • 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. – feeela Jul 05 '12 at 12:25

1 Answers1

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);
 }
Community
  • 1
  • 1
Nemanja
  • 1,505
  • 12
  • 24
  • 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? – Chris Jul 05 '12 at 11:39
  • 1
    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. – Nemanja Jul 05 '12 at 11:42
  • hey, you don't have to scribble sth, really! what about just using `checkdate()` to verify the correct date? – Chris Jul 05 '12 at 11:44
  • 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. – Nemanja Jul 05 '12 at 11:57
  • 1
    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. – Joel Mellon Jan 29 '14 at 19:56