如何在 Laravel PHP 中格式化日期 Carbon:now()

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

How format date Carbon:now() in Laravel PHP

phplaravelformatdatetime

提问by ThaiVD

I tried

我试过

 update(['access' => Carbon::now()->format('Y/m/d H:i:s')]);

it returned Y-m-d H:i:s

它回来了 Y-m-d H:i:s

回答by Lovepreet Singh

The default format in mysql is Y-m-d H:i:s, so it will save and show in the same format. If you still want to store it as Y/m/d H:i:s, then change data type of date field to varchar.

mysql 中的默认格式是Y-m-d H:i:s,所以它会以相同的格式保存和显示。如果您仍想将其存储为Y/m/d H:i:s,则将日期字段的数据类型更改为 varchar。

For issue mentioned in comment:

对于评论中提到的问题:

Create date objectfrom string first using date_createand then use date_formatto change its format.

date object首先使用date_create从字符串创建,然后使用date_format更改其格式。

$date=date_create($member['access']);
echo date_format($date,"Y/m/d H:i:s");

回答by DsRaj

You update this in the database and from the database, you will return only Y-m-d H:i:s(Mysql default format) so

你在数据库和数据库中更新它,你将只返回 Ymd H:i:s(Mysql 默认格式) 所以

While printing the value of accesschange the format what you needed.

在打印access更改格式的值时,您需要什么。

echo date('Y/m/d H:i:s',strtotime($member['access']));