Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetch date from Carbon\Carbon Object

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?

like image 529
Rob Avatar asked Apr 04 '18 17:04

Rob


People also ask

How do I use datetime carbon in PHP?

Carbon - A simple PHP API extension for DateTime. The Carbon class is inherited from the PHP DateTime class. You can see from the code snippet above that the Carbon class is declared in the Carbon namespace. You need to import the namespace to use Carbon without having to provide its fully qualified name each time.

How do I print a carbon instance as a date string?

You'll notice the __toString () method is defined which allows a Carbon instance to be printed as a pretty date time string when used in a string context. You can also set the default __toString () format (which defaults to Y-m-d H:i:s) thats used when type juggling occurs. As part of the settings 'toStringFormat' can be used in factories too.

Where does the carbon class come from in PHP?

The Carbon class is inherited from the PHP DateTime class. <?php namespace Carbon ; class Carbon extends DateTime { // code here }. You can see from the code snippet above that the Carbon class is declared in the Carbon namespace.

How are timezones handled in carbon 2?

Starting with Carbon 2, timezones are now handled with a dedicated class CarbonTimeZone extending DateTimeZone.


Video Answer


1 Answers

$analyticsData = Analytics::fetchVisitorsAndPageViews(Period::days(7));

   foreach($analyticsData as $data)
   {
       print_r($data['date']->toDateString());die;
   }
like image 156
Rob Avatar answered Oct 17 '22 03:10

Rob