Reputation: 2164
I am getting a Collection which contains
[80] => Array
(
[date] => Carbon\Carbon Object
(
[date] => 2018-04-04 17:27:24.000000
[timezone_type] => 3
[timezone] => UTC
)
I want to get the date from here, when I do foreach it gives a Carbon\Carbon Object('date' like this, but then can not access date.
Does anyone know any solution?
Upvotes: 6
Views: 9782
Reputation: 69
If you like to convert to string, use
$data['date']->toDateTimeString()
Or you can format custom
$data['date']->format('Y/m/d H:i')
Upvotes: 3
Reputation: 2164
$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));
foreach($analyticsData as $data)
{
print_r($data['date']->toDateString());die;
}
Upvotes: 3
Reputation: 4668
Carbon is a nice wrapper around dates. It has a really great API to get things like date subtraction, testing if a date is in a range, formatting dates, etc.
It sounds like you're looking to format an array of Carbon
objects to a date.
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);
Note: I'm assuming your array is named $dates
. If you want a more accurate answer, provide the whole output of your collection along with the name of the variable.
To try out other date formatting options check out their (awesome) documentation: https://carbon.nesbot.com/docs/#api-formatting
Upvotes: 0