Search code examples
phplaravelphp-carbon

How do I convert `June, 18 2021 21:33` to `2021-06-18 21:33:00` in PHP?


This is simple to some;

I got the time: June, 18 2021 21:33

How do I convert into 2021-06-18 21:33:00 in PHP.

I tried:

$send_time = "June, 18 2021 21:33";
    
$date = Carbon::parse($send_time);

echo $date->format('Y-m-d h:i:sa');

Then got Could not parse 'June, 18 2021 21:33': DateTime::__construct(): Failed to parse time string (June, 18 2021 21:33) at position 6 (1): Unexpected character


Solution

  • That datetime is not in any standard format. You're going to have to create your own format to pass in:

    $date = Carbon::createFromFormat('F, d Y H:i', $send_time);
    

    See https://www.php.net/manual/en/datetime.format.php for all of the date format characters