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!

4
  • Carbon::parse() should either return a Carbon instance or an "InvalidFormatException". Carbon::parse($fillingDate, 'UTC') should give back a full Carbon instance Commented Dec 14, 2020 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", } Commented Dec 14, 2020 at 19:18
  • Does this answer your question? How could i create carbon object from given datetime structure? Commented Dec 14, 2020 at 22:40
  • See also stackoverflow.com/questions/34971082/… Commented Dec 14, 2020 at 22:40

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

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.