laravel 如何将碳转换为字符串,仅取日期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50692147/
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 17:47:24  来源:igfitidea点击:

How to convert a carbon into string, to take the date only?

phplaravelcollectionsphp-carbon

提问by Almaida Jody

i have an collection like this

我有一个这样的收藏

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"
]

how to take the "2018-05-28" only? can somebody help me to fix this problem? thank's anyway

如何只取“2018-05-28”?有人可以帮我解决这个问题吗?不管怎么说,还是要谢谢你

回答by Ziyad

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

回答by Mohammed Omer

i prefere this

我更喜欢这个

$model_item->created_at->toDateString();

since all date fields that located in protected $dates arrayin the modal is type of \Carbon\Carbon.

因为位于protected $dates array模式中的所有日期字段都是\Carbon\Carbon.

so the above code could apply to any date field as long as you declared it as date in $dates arrayin the modal as follow

所以上面的代码可以应用于任何日期字段,只要你$dates array在模态中将其声明为日期,如下所示

// this includes created_at and updated_at by default so no need to add it 
protected $dates = ['approved', 'seen_at];