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

  • SEARCH
  • Home
  • 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 7964453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T05:53:47+00:00 2026-06-04T05:53:47+00:00

An easy follow up From an earlier question ( Date functions in PHP )

  • 0

An easy follow up From an earlier question ( Date functions in PHP ) I have the following code:

$date_str = "Jan 14th 2011";
$date = new DateTime($date_str);
echo $date->format('d-m-y');

What I am wondering is if there is an easy way to check if $date_str will convert to a date so that I can stop prevent the error when it fails?

Basically I am looking to avoid using try catch statements but perhaps that is not possible.

  • 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-04T05:53:48+00:00Added an answer on June 4, 2026 at 5:53 am

    As per the docs, the DateTime constructor will throw an exception if the date can’t be parsed properly. So…

    try {
        $date = new DateTime($date_str);
    } catch (Exception $e) {
        die("It puked!");
    }
    

    If you’re using the procedural interface, you’ll get a boolean false instead, so…

    $date = date_create_from_format(...);
    if ($date === FALSE) {
        die("It puked!");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could I get very easy to follow code examples on the following: Use browser
As a follow up to this question: Is there an easy way to pickle
I have some code that deletes multiple objects from Mongo using an $or opperator,
This is a follow up question from here specifically concerning its answer . From
Debug HTTP is easy, you have all sort of tools to do it (like
Here's the question first: Is this possible? I'm taking my inspiration from Joe Wrobel's
I'm developing an enterprise-like application that fetches data from a remote server. I have
I am trying to capture the output from tail in follow mode, where it
As a follow-up to this question , it's trying to find out how to
I'm learning about deadlocks in Java, and there's this sample code from Sun's official

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.