laravel carbon get day names from a date column from a collection
Asked Answered
G

5

13

I have this collection from a database where I have a column called "event_date".

What I want is just to get only the day names from that column that comes in a collection.

I know you can use Carbon to get the name days through, for example, a method called ->format(), however I get an error saying that this method does not exist.

My code so far is as follows:

$collection = MyModel::all();

Inside there is the "event_date" property or column. From that, I want to get the names of the days to put them into an array or collection and finally count those days frequencies.

In order to achieve this I have tried the following:

I tried the ->pluck() method as follows:

$filtered = collect([
            'myDates'=>$collection->pluck('event_date'),
        ]);

And the dd($filtered) looks like as follows:

Collection {#209 ▼
  #items: array:1 [▼
    "myDates" => Collection {#243 ▼
      #items: array:30 [▼
        0 => Carbon {#244 ▼
          +"date": "2017-02-05 00:00:00.000000"
          +"timezone_type": 3
          +"timezone": "America/Mexico_City"
        }
        1 => Carbon {#218 ▼
          +"date": "2017-01-15 00:00:00.000000"
          +"timezone_type": 3
          +"timezone": "America/Mexico_City"
        }
        2 => Carbon {#250 ▼
          +"date": "2016-09-25 00:00:00.000000"
          +"timezone_type": 3
          +"timezone": "America/Mexico_City"
        }
        3 => Carbon {#249 ▼
          +"date": "2016-05-22 00:00:00.000000"
          +"timezone_type": 3
          +"timezone": "America/Mexico_City"
        }
...

I tried to get the day names as follows:

$Daynames = $filtered->each(function($item,$key){
            return $item->myDates->format('l');
        });

But I got the following error:

Undefined property: Illuminate\Support\Collection::$myDates

Any ideas to get only the daynames into an array or collection? Is there another way to do this?

Greerson answered 17/2, 2017 at 4:4 Comment(0)
S
20

You're calling format() on the collection of dates, while you should call it on the Carbon objects. You need another loop that would go through all dates in myDates collection and format them, e.g.:

$Daynames = [];
$filtered->each(function($item,$key) use (&$Daynames) {
  $item->each(function($date) use (&$Daynames) {
    $Daynames[] = $date->format('l');
  });
});
Spital answered 17/2, 2017 at 4:22 Comment(1)
Nice! But why do I get the $Daynames empty? If I echo out each $date->format('l') I can see the day names. But the array appears empty. I have tried use($Daynames) and return $Daynames; and it does not work either.Greerson
N
9

You can do with with more than one way

Carbon::parse($date)->dayName; // Monday ... 

Carbon::parse($date)->format('l'); // Monday ... 

custom location

Carbon::parse($date)->locale('fr')->dayName;// Lundi ...
Natatorium answered 23/12, 2021 at 16:40 Comment(1)
People should see this too.Folkways
D
6

By Carbon library we can get it easly.

  1. Use Library

    use Carbon\Carbon;
    
  2. Get date

    $date = '2018-11-15';
    $d    = new DateTime($date);
    $d->format('l');  //pass l for lion aphabet in format 
    

Output is Thursday. It worked for me in laravel

Dodge answered 15/11, 2018 at 12:52 Comment(2)
What's the use Carbon\Carbon; command to get the same result.Lamellirostral
Why importing Carbon?Nunnery
C
6
  1. Use the library

    use Carbon\Carbon;

  2. Parsing the date

    $day = Carbon::parse($yourDate)->format('l')

Clowers answered 3/3, 2020 at 6:20 Comment(0)
G
2

nice and elegant way to achieve this using collection pipelines:

$days = MyModel::all()->pluck('event_date')->map(function($date) {
    return $date->format('l');
}); 
Giacobo answered 17/2, 2017 at 6:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.