laravel carbon 从集合的日期列中获取日期名称

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42289199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 15:20:41  来源:igfitidea点击:

laravel carbon get day names from a date column from a collection

phplaravellaravel-5php-carbon

提问by Pathros

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

我从一个数据库中收集了这个集合,其中有一个名为“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.

我知道您可以使用 Carbon 来获取名称 days 通过,例如,一个名为 的方法->format(),但是我收到一个错误消息,指出此方法不存在。

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.

里面有“event_date”属性或列。从那以后,我想获取日期的名称,将它们放入数组或集合中,最后计算这些日期的频率。

In order to achieve this I have tried the following:

为了实现这一点,我尝试了以下方法:

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

我尝试了->pluck()如下方法:

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

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

dd($filtered)看起来像如下:

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

未定义的属性:Illuminate\Support\Collection::$myDates

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

有什么想法可以只将日期名称放入数组或集合中吗?有没有其他方法可以做到这一点?

回答by jedrzej.kurylo

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

您在日期集合上调用format(),而您应该在Carbon对象上调用它。您需要另一个循环来遍历myDates集合中的所有日期并对其进行格式化,例如:

$Daynames = [];
$filtered->each(function($item,$key) use (&$Daynames) {
  $item->each(function($date) use (&$Daynames) {
    $Daynames[] = $date->format('l');
  });
});

回答by amba patel

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 
    
  1. 使用图书馆

    use Carbon\Carbon;
    
  2. 获取日期

    $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

输出是Thursday。它在 Laravel 中对我有用

回答by Almazik G

nice and elegant way to achieve this using collection pipelines:

使用收集管道实现这一目标的好方法:

$days = MyModel::all()->pluck('event_date')->map(function($date) {
    return $date->format('l');
}); 

回答by Elbo S.P.

  1. Use the library

    use Carbon\Carbon;

  2. Parsing the date

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

  1. 使用图书馆

    use Carbon\Carbon;

  2. 解析日期

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