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.
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);
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.
An API extension for DateTime that supports 281 different languages.
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.
you should use the public function toDateTimeString()
echo $recordSet['created_at']->toDateTimeString();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With