Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8174723
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:32:07+00:00 2026-06-06T22:32:07+00:00

Does anybody know why this function, when passed an invalid date (e.g. timestamp) to

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:32:08+00:00Added an answer on June 6, 2026 at 10:32 pm

    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);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anybody know why I get this warning when I turn off the auto-commit
Does anybody know how to do this syntax below in mysql? Without Stored Procedure,
Does anybody know why the same code from this page http://emacsformacosx.com/ would not render
Does anybody know whether one exists? I've been googling this for monthes... Thanks
I found some information about this on Scott Hanselmans Blog Does anybody exactly know
In Oracle, this returns 03/01/2010. That does not make sense to me. Anybody know
Does anybody know how this PHP code is encoded? I don't know whether it's
Does anybody know why the following code only has effect in FF? $(document).ready(function() {
Does anybody know how many emails are limited in the mail() function? I just
Does anybody know how to create a function import in an Entity Data Model

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.