Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Carbon object type?

Given the following code:

$recordSets = Model::find(1)->get();

foreach ($recordSets as $recordSet) {
  dd($recordSet['created_at']);
}

I got this result.

object(Carbon\Carbon)[292]
  public 'date' => string '2013-08-21 17:05:19' (length=19)
  public 'timezone_type' => int 3
  public 'timezone' => string 'UTC' (length=3)

I tried to access the 'date' using

echo $recordSet['created_at']->date;

But I got an error:

Unknown getter 'date'

How to access $recordSet['created_at']? It is just for formatting of date/time purpose.

like image 476
Artisan Avatar asked Aug 22 '13 04:08

Artisan


People also ask

How do you find the date on a Carbon object?

You can use array_map to produce this kind of result: $dates_formatted = array_map(function($entry) { // transform the Carbon object to something like 'Dec 25, 1975' return $entry['date']->toFormattedDateString(); }, $dates);

What is Carbon :: now ()?

Carbon::now returns the current date and time and Carbon:today returns the current date. $ php today.php 2022-07-13 15:53:45 2022-07-13 00:00:00. This is a sample output. Carbon::yesterday creates a Carbon instance for yesterday and Carbon::tomorrow for tomorrow.

What is Nesbot Carbon?

An API extension for DateTime that supports 281 different languages.

What is Carbon class in laravel?

Carbon is a package by Brian Nesbit that extends PHP's own DateTime class. It provides some nice functionality to deal with dates in PHP. Specifically things like: Dealing with timezones. Getting current time easily.


2 Answers

you should use the public function toDateTimeString()

echo $recordSet['created_at']->toDateTimeString();
like image 189
Sherif Avatar answered Oct 20 '22 15:10

Sherif


Simply use $recordSet['created_at'].

Because of a __toString method in Carbon, read $recordSet['created_at'] will always return the date under string format.

If you want to see which method you can use, see vendor/nesbot/carbon/Carbon/Carbon.php

like image 34
netvision73 Avatar answered Oct 20 '22 14:10

netvision73