0

I am using Laravel Framework 6.16.0 and want to parse a date with carbon "Dec 14 02:04 PM":

$fillingDate = "Dec 14 02:04 PM";
$filling_Date = Carbon::parse($fillingDate, 'UTC'); 
// result is only a string "Dec 14 02:04 PM"

When using the above structure I only get the string back. However, I would like to get a Carbon object that gets then formatted to ->format('Y-m-d').

Any suggestions what I am doing wrong?

I appreciate your replies!

Carol.Kar
  • 4,581
  • 36
  • 131
  • 264
  • `Carbon::parse()` should either return a Carbon instance or an "InvalidFormatException". `Carbon::parse($fillingDate, 'UTC')` should give back a full Carbon instance – Donkarnash Dec 14 '20 at 19:18
  • Did you do `dd($filling_date)`? It _is_ a Carbon instance in my code: `=> Carbon\Carbon @1607954640 {#3300 date: 2020-12-14 14:04:00.0 UTC (+00:00), timezone_type: 3, timezone: "UTC", }` – Tim Lewis Dec 14 '20 at 19:18
  • Does this answer your question? [How could i create carbon object from given datetime structure?](https://stackoverflow.com/questions/41236385/how-could-i-create-carbon-object-from-given-datetime-structure) – miken32 Dec 14 '20 at 22:40
  • See also https://stackoverflow.com/questions/34971082/create-date-carbon-in-laravel – miken32 Dec 14 '20 at 22:40

1 Answers1

3

You can create carbon object from a string by calling the static function createFromFormat.

In this case:

$fillingDate = "Dec 14 02:04 PM";    
$filling_Date = Carbon::createFromFormat("M d g:i A", $fillingDate)

Format characters explained:

  • M - A short textual representation of a month, three letters
  • d - Day of the month, 2 digits with leading zeros
  • g - 12-hour format of an hour without leading zeros
  • i - Minutes with leading zeros
  • A - Uppercase Ante meridiem and Post meridiem

More about format characters: PHP documentation

fff
  • 402
  • 3
  • 10