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
Carbon parse date format
提问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:00
but instead of +01:00
you'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;
}