laravel 使用 Carbon 更改日期格式

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

Change Date format using Carbon

phplaraveldatephp-carbon

提问by scott

I am using Laravel framework and for date conversion using Carbon package

我正在使用 Laravel 框架并使用 Carbon 包进行日期转换

I am unable convert date format to mysql format.I have following code

我无法将日期格式转换为 mysql 格式。我有以下代码

$request->event_start_datewill have 25/08/2017

$request->event_start_date会有 25/08/2017

print_r(carbon::parse($request->event_start_date));

when $request->event_start_dateis 03/08/2017then it will print as

$request->event_start_date03/08/2017那么它将打印成

Carbon\Carbon Object( [date] => 2017-03-08 00:00:00.000000 [timezone_type] => 3 [timezone] => UTC)

But if date is 25/08/2017then it will throw erorr as

但是如果日期是25/08/2017那么它会抛出错误

"G:\XAMPP\htdocs\myproject\vendor\nesbot\carbon\src\Carbon\Carbon.php" line : 291 message : "DateTime::__construct(): Failed to parse time string (25/08/2017) at position 0 (2): Unexpected character"

“G:\XAMPP\htdocs\myproject\vendor\nesbot\carbon\src\Carbon\Carbon.php”行:291 消息:“DateTime::__construct(): 无法解析时间字符串 (25/08/2017) at位置 0 (2):意外字符”

need to convert 25/08/2017to Mysql date format.I have tried a lot to fix this .finaly posted here so that i get some help from you

需要转换25/08/2017为 Mysql 日期格式。我已经尝试了很多来解决这个问题。finaly 在这里发布,以便我从你那里得到一些帮助

Thanks

谢谢

回答by iainn

Carbon extends PHP's native DateTimeclass, so you can use the same createFromFormatmethod:

Carbon 扩展了 PHP 的原生DateTime类,因此您可以使用相同的createFromFormat方法:

$dateString = '25/08/2017';
$dateObject = \Carbon::createFromFormat('d/m/Y', $dateString);