1

Hi I want to create DateTime object from date 1989-06-31. As you can see this date has bad count of days(June can't have 31 days). But calling this

$semi_valid_date = "1989-06-31";
$semi_valid_datetime = DateTime::createFromFormat('Y-m-d',$semi_valid_date);
$valid_date = $semi_valid_datetime->format('Y-m-d');

will store to $valid_date date 1989-07-01.

Is it possible to throw error, when month has more days, than it can have?

1
  • 6th month or aka June doesn't have 31 days. That's way first next day is selected after 06-30, which is 07-01. Commented Jan 30, 2014 at 20:57

1 Answer 1

1

Use checkdate() to validate the date before attempting to use it:

var_dump(checkdate(6, 31, 1989));
// bool(false)

var_dump(checkdate(6, 30, 1989));
// bool(true)

See it in action

Sign up to request clarification or add additional context in comments.

Comments

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.