【问题标题】:laravel carbon get day names from a date column from a collectionlaravel carbon 从集合的日期列中获取日期名称
【发布时间】:2017-07-06 11:08:43
【问题描述】:

我从一个数据库中收集了这个集合,其中有一个名为“event_date”的列。

我想要的只是从集合中的该列中获取日期名称。

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

到目前为止我的代码如下:

$collection = MyModel::all();

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

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

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

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

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"
        }
...

我尝试按如下方式获取日期名称:

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

但我收到以下错误:

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

有什么想法可以只将日期名放入数组或集合中吗?还有其他方法吗?

【问题讨论】:

    标签: php laravel laravel-5 php-carbon


    【解决方案1】:

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

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

    【讨论】:

    • 不错!但是为什么我的$Daynames 是空的?如果我回显每个$date->format('l'),我可以看到日期名称。但是数组显示为空。我试过use($Daynames)return $Daynames; 也没有用。
    【解决方案2】:

    通过 Carbon 库我们可以很容易地得到它。

    1. 使用库

      use Carbon\Carbon;
      
    2. 获取日期

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

    输出为Thursday。 它在 laravel 中对我有用

    【讨论】:

    • 碳\碳有什么用;命令得到相同的结果。
    • 为什么要进口 Carbon?
    【解决方案3】:
    1. 使用库

      use Carbon\Carbon;

    2. 解析日期

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

    【讨论】:

      【解决方案4】:

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

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

      【讨论】:

        【解决方案5】:

        你可以用不止一种方法来做

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

        自定义位置

        Carbon::parse($date)->locale('fr')->dayName;// Lundi ...
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-09-28
          • 2015-02-24
          • 1970-01-01
          • 2015-10-21
          • 2017-04-17
          • 2019-06-18
          • 2021-08-21
          • 1970-01-01
          相关资源
          最近更新 更多