0

I tried to research regarding this issue but didn't see exactly like this since they're asking the reverse format. I'm trying to format readable date to any format but it is returning an error. Please see details below.

String date

23 June, 2021 02:00 PM

Need to convert to

06-23-2021 14:00:00

Code

Carbon::parse('23 June, 2021 02:00 PM');

Error

Could not parse '23 June, 2021 02:00 PM': DateTime::__construct(): Failed to parse time string (23 June, 2021 10:00 AM) at position 14 (1): Double time specification
Blues Clues
  • 1,694
  • 3
  • 31
  • 72

1 Answers1

1

dont know if its useful for you or not, but in simple php you can do this: (comma can not be present unless you remove it somehow)

$datestr = "23 June 2021 02:00 PM";

$date = new DateTime($datestr);
echo $date->format('Y-m-d H:i:s');

and the result would look like: **2021-06-23 14:00:00**

vins
  • 449
  • 4
  • 13
  • Try to add `,` for the date. It will return an error. Try this `23 June, 2021 02:00 PM` – Blues Clues Jun 22 '21 at 10:43
  • thats why i said, without comma(,) well i am not sure if there is other way around but i think you could remove the comma (assuming date to be string as you said!). Otherwise i think you could covert it to stringify the date and proceed ... which definitely is NOT OPTIMAL SOLUTION...I hope other experts could help you out here... – vins Jun 22 '21 at 10:46
  • yea, i just need to remove the comma using str_replace. – Blues Clues Jun 22 '21 at 10:49