0

I need to check a string is match with some format, and i using DateTime::createFromFormat to enforce it. but it got some bugs.

example i have an date: 8/15/2020, i try to parse it to datetime object with format "d/m/Y", then i print it in to other format:

   DateTime::createFromFormat("d/m/Y", '8/15/2020')->format('d-m-Y')
   // result: 08-03-2021

that is wreig, because i read in php.net https://www.php.net/manual/en/datetime.createfromformat.php they said: d: day (1->30), m: month (1->12), Y: year (2020)

But i got an strange result? how it work? or it is the bug?

thank you for your answer.

2
  • Strange? Please do not speculate. Check with the documented behavior and explain what you obtain constitutes an exception to the rule. Also please take a look for existing material for that matter on the site. Commented Jul 5, 2021 at 5:00
  • 1
    Check out php.net/manual/en/datetime.getlasterrors.php Commented Jul 5, 2021 at 7:58

3 Answers 3

1

I think the PHP manual is not correct.

The DateTime::getLastErrors() method generates a warning if the range for the day or month is exceeded. You can evaluate this. As an an example:

function DateTimeCreateFromStrictFormat($format, $input){
  $dt = DateTime::createFromFormat($format, $input);
  $errArr = DateTime::getLastErrors();
  return $errArr['warning_count']+$errArr['error_count'] ? false : $dt;
}
$dt = DateTimeCreateFromStrictFormat("d/m/Y", '8/15/2020');
var_dump($dt);  //bool(false)

$dt = DateTimeCreateFromStrictFormat("d/m/Y", '8/12/2020');
var_dump($dt);  //object(DateTime)#2 (3) { ["date"]=> string(26) "2020-12-08 10:34:34.000000" ..
Sign up to request clarification or add additional context in comments.

Comments

1

Your format is day/month/year but there is no month 15 like the 8/15/2020 being given.

Did you mean 15/8/2020?

2 Comments

yes,so i want it return false to me, bacuse no month 15. but it doesn't
1

i try to parse it to datetime object with format "d/m/Y"

... it's not clear why you tried it like that. 8/15/2020 is clearly m/d/Y. You need to parse it using the format it's actually written in.

You can output it again to d/m/Y afterwards - that is a separate operation.

DateTime::createFromFormat("m/d/Y", '8/15/2020')->format('d-m-Y')

3 Comments

no, actually i want to check is the string ('8/15/2020') fit with the format "d/m/Y". and i try by using DateTime::createFromFormat function. but seem it still work end return to me an unexpected date
That wasn't clear from your question. Unfortunately you can't use createFromFormat as a validation tool in that way - it will always try to do its best and create a date from the data provided, even if it doesn't seem to make sense. To validate the format strictly you probably need to use some sort of regular expression.
“Unfortunately you can't use createFromFormat as a validation tool in that way” - php.net/manual/en/datetime.getlasterrors.php can tell you that something went wrong though.

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.