Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shamshashmi's avatar

Carbon::createFromFormat dropping

Hi Everyone, I'm having an issue since 2 days and im researching to find a solution for this. The problem is, I'm reading Mysql timestamps and laravel converting those stamps to carbon instance by default. But when it runs Carbon::createFromFormat($this->getDateFormat(), $value) this function and if the time in timestamp is higher than 12 (e.g 16:00:00) then it drops with error message saying "Failed to parse time string (2017-09-25 16:50 PM) at position 17 (P): The timezone could not be found in the database" ... Any help will be highly appreciated.

Regards

0 likes
9 replies
tykus's avatar

If $this->getDateFormat() returns a format using the lowercaseh for hours, then Carbon is not expecting 24 hour times - you need to use the 24 hour formatter H when parsing the datetime from the database.

shamshashmi's avatar

yes, its 'Y-m-d H:i:s' so its already 24hrs format

36864's avatar

Your time string is invalid.

16:50 PM would be 16 hours and 50 minutes after noon.

rin4ik's avatar

2017-09-25 16:50 PM is not equal to this 'Y-m-d H:i:s'. at position 17 (P) it can't read PM. it expects seconds

tykus's avatar

But your datetime string 2017-09-25 16:50 PM is not in that format - so you are telling it to use a particular format (Y-m-d H:i:s) to parse the string, but then you are not giving a datetime string in the same format. How can this be expected to work?

shamshashmi's avatar

See in tinker i ran same command

  • Carbon\Carbon::createFromFormat('Y-m-d G:i:s', '2018-04-04 16:00:00');
  • Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2018-04-04 16:00:00')

same error im getting in both cases....

shamshashmi's avatar

It was the Php version issue with Carbon api.... can not work with php > 7.1.

36864's avatar

Could you be a little more specific? What version of Carbon are you using and what version of PHP does it not work on?

Please or to participate in this conversation.