如何将碳转换为字符串,仅保留日期?

问题描述 投票:4回答:2

我有这样的收藏集

0 => array:4 [
  "so" => "SO-1"
  "product_name" => "EXTREME FORTE - BP"
  "created_at" => Carbon @1527481346 {#628
    date: 2018-05-28 04:22:26.0 UTC (+00:00)
  }
  "id" => "5b0b84027475aa1508002623"
]

如何只参加“ 2018-05-28”?有人可以帮我解决这个问题吗?还是谢谢你

php laravel collections php-carbon
2个回答
7
投票
$collection_item->created_at->format('Y-m-d');

1
投票

我更喜欢这个

$model_item->created_at->toDateString();

因为模态中位于protected $dates array的所有日期字段都是\Carbon\Carbon的类型。

因此,只要您在模式中的$dates array中将其声明为日期,上述代码就可以应用于任何日期字段如下]

// this includes created_at and updated_at by default so no need to add it 
protected $dates = ['approved', 'seen_at];
© www.soinside.com 2019 - 2024. All rights reserved.