如何将碳转化为字符串,只取日期?

2022-08-30 18:56:22

我有一个这样的收藏

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”?有人可以帮我解决这个问题吗?无论如何,谢谢


答案 1
$collection_item->created_at->format('Y-m-d');

答案 2

我更喜欢这个

$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'];

推荐