laravel 碳解析日期格式

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

Carbon parse date format

phplaraveldatedatetimephp-carbon

提问by Louis R

I'm trying to parse a date formated like :

我正在尝试解析格式如下的日期:

2017-09-20T10:59:10.0000000 01:00

I'm using Carbon, so i tried :

我正在使用 Carbon,所以我尝试了:

Carbon::createFromFormat('Y-m-dTH:i:s.u vP', $date)

Which output :

哪个输出:

The timezone could not be found in the database\n
Unexpected data found.\n
Data missing

I guess the last timezone argument maybe wrong but i couldn't find how to parse that date format :/

我猜最后一个时区参数可能是错误的,但我找不到如何解析该日期格式:/

Thanks for help !

感谢帮助 !

采纳答案by Alexey Mezenin

You'll need to add a sign to the timezone, for example:

您需要为时区添加一个标志,例如:

+01:00

Then this will work for you:

那么这对你有用:

Carbon::createFromFormat('Y-m-d\TH:i:s.0000000 P', $date)

If your string can have -01:00but instead of +01:00you're getting 01:00, do this first:

如果您的字符串可以有-01:00但不是+01:00您得到01:00,请先执行以下操作:

$timezone = str_after($date, ' ');
if ($timezone[0] !== '-') {
    $date = str_before($date, ' ') . ' +' . $timezone;
}