php Laravel 5 Carbon 格式日期时间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33405939/
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
Laravel 5 Carbon format datetime
提问by d3bug3r
I have an array that returns the following date time:
我有一个返回以下日期时间的数组:
$item['created_at'] => "2015-10-28 19:18:44"
How do I change the date to M d Y
format in Laravel using Carbon?
如何M d Y
使用 Carbon 在 Laravel 中更改日期格式?
Currently it returns with an error
目前它返回一个错误
$suborder['payment_date'] = $item['created_at']->format('M d Y');
回答by Milan Maharjan
First parse the created_at field as Carbon object.
首先将 created_at 字段解析为 Carbon 对象。
$createdAt = Carbon::parse($item['created_at']);
Then you can use
然后你可以使用
$suborder['payment_date'] = $createdAt->format('M d Y');
回答by Sophy
Date Casting for Laravel 6.x and 7.x
Laravel 6.x 和 7.x 的日期转换
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'created_at' => 'datetime:Y-m-d',
'updated_at' => 'datetime:Y-m-d',
'deleted_at' => 'datetime:Y-m-d h:i:s'
];
It easy for Laravel 5 in your Model add property protected $dates = ['created_at', 'cached_at']
. See detail here https://laravel.com/docs/5.2/eloquent-mutators#date-mutators
Laravel 5 很容易在您的 Model 添加属性中protected $dates = ['created_at', 'cached_at']
。在此处查看详细信息https://laravel.com/docs/5.2/eloquent-mutators#date-mutators
Date Mutators: Laravel 5.x
日期修改器:Laravel 5.x
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['created_at', 'updated_at', 'deleted_at'];
}
You can format date like this $user->created_at->format('M d Y');
or any format that support by PHP.
您可以像这样格式化日期$user->created_at->format('M d Y');
或 PHP 支持的任何格式。
回答by fajarhac
If you are using eloquent model (by looking at your code, i think you are), you dont need to convert it into array. Just use it as object. Becaus elike Thomas Kim said, by default it is a Carbon instance
如果您使用的是 eloquent 模型(通过查看您的代码,我认为您是),则不需要将其转换为数组。只需将其用作对象即可。因为像 Thomas Kim 说的,默认情况下它是一个 Carbon 实例
So it should be
所以应该是
$suborder['payment_date'] = $item->created_at->format('Y-m-d')
But if it is not then, you need convert it to Carbon object as Milan Maharjan answer
但如果不是,则需要将其转换为 Carbon 对象,如 Milan Maharjan 的回答
$createdAt = Carbon::parse($item['created_at']);
回答by Sanith
$suborder['payment_date'] = Carbon::parse($item['created_at'])->format('M d Y');
回答by Filipe Cruz
Declare in model:
在模型中声明:
class ModelName extends Model
{
protected $casts = [
'created_at' => 'datetime:d/m/Y', // Change your format
'updated_at' => 'datetime:d/m/Y',
];
回答by Tushar
Laravel 5 timestamps are instances of Carbon class, so you can directly call Carbon's string formatting method on your timestamps. Something like this in your view file.
Laravel 5 时间戳是 Carbon 类的实例,因此您可以直接在时间戳上调用 Carbon 的字符串格式化方法。在您的视图文件中是这样的。
{{$task->created_at->toFormattedDateString()}}
回答by li bing zhao
Try that:
试试看:
$createdAt = Carbon::parse(date_format($item['created_at'],'d/m/Y H:i:s');
$createdAt= $createdAt->format('M d Y');
回答by Anjani Barnwal
just use
只是使用
Carbon::createFromFormat('Y-m-d H:i:s', $item['created_at'])->format('M d Y');
回答by DAVID AJAYI
Just use the date()
and strtotime()
function and save your time
只需使用date()
和strtotime()
功能即可节省您的时间
$suborder['payment_date'] = date('d-m-Y', strtotime($item['created_at']));
Don't stress!!!
不要紧张!!!