laravel 将日期时间转换为 PHP

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

Convert Datetime to PHP

phplaravellaravel-4php-carbon

提问by Gagan

I have a datetime string as such 14/04/2014 4:57:16 PMand I am using carbon datetime to work on it.

我有一个日期时间字符串14/04/2014 4:57:16 PM,我正在使用碳日期时间来处理它。

However as soon as I try to do this

但是,一旦我尝试这样做

$dt = Carbon::createFromTimestampUTC($encDateTime);

However I am getting parse errors : DateTime::__construct(): Failed to parse time string (@14/04/2014 5:03:45 PM ) at position 3 (/): Unexpected character.

但是我收到解析错误:DateTime::__construct(): 无法解析位置 3 (/) 处的时间字符串 (@14/04/2014 5:03:45 PM):意外字符。

Udpate :I tried doing this :

更新:我尝试这样做:

$date = DateTime::createFromFormat('d/m/Y H:i A', $encDateTime);
dd($date->format('Y-m-d H:i:s'));

and I get the following error Call to a member function format() on a non-object

我收到以下错误 调用非对象上的成员函数 format()

Can you please help me out with this?

你能帮我解决这个问题吗?

Thanks

谢谢

回答by Amal Murali

Your date string contains a space at the end - that's why DateTime::createFromFormat()fails to parse it. To remove the extra whitespace from the beginning and/or end, you could use trim().

您的日期字符串末尾包含一个空格 - 这就是DateTime::createFromFormat()无法解析它的原因。要从开头和/或结尾删除多余的空格,您可以使用trim().

Also, you were originally using H, which is the 24-hour format with leading zeroes. In this case, it looks like you have a 12-hour format, so use ginstead (hwould work, too).

此外,您最初使用的是H,这是带有前导零的 24 小时格式。在这种情况下,您的格式看起来像是 12 小时制,因此请g改用 (h也可以)。

The following should work:

以下应该工作:

$date = DateTime::createFromFormat('d/m/Y g:i:s A', trim($encDateTime));

When you receive such errors, always var_dump()the values. That way, you can check if the variable contains what you think it does.

当您收到此类错误时,始终var_dump()是 values。这样,您可以检查变量是否包含您认为的内容。

回答by hannesvdvreken

The static method createFromFormatfrom DateTime returns NULLwhen there is a parse error. The format that was given (d/m/Y H:i A) was wrong. Hence the error "Call to a member function format()on a non-object".

createFromFormatDateTime 中的静态方法NULL在出现解析错误时返回。给出的格式 ( d/m/Y H:i A) 是错误的。因此出现错误“调用format()非对象上的成员函数”。

You can create DateTime objects from non-standard timestrings like this:

您可以像这样从非标准时间字符串创建 DateTime 对象:

$time = '14/04/2014 4:57:16 PM';
$date = Carbon\Carbon::createFromFormat('d/m/Y g:i:s A', $time);

The different format characters available and their explanations can be found in the documentation for date().

可用的格式不同的人物和他们的解释中可以找到该文件date()

回答by skrilled

I don't know anything about Carbon, but this works. You were using a two digit (H) instead of h (edit: g is the same exact thing) in your createFromFormat method:

我对碳一无所知,但这有效。您在 createFromFormat 方法中使用了两位数 (H) 而不是 h (编辑:g 是完全相同的):

$encDateTime = "14/04/2014 4:57:16 PM";

$date1 = DateTime::createFromFormat('d/m/Y h:i:s A', trim($encDateTime));
echo $date1->format('Y-m-d H:i:s');

result:

结果:

2014-04-14 16:57:16

回答by james

just use laraveldate function as:

只需将laravel日期函数用作:

$productsales=DB::table('tbl_trans')   
     ->leftjoin('tbl_product','tbl_product.prod_id','=','tbl_trans.product_id')  
     ->select(DB::raw('count(product_id) as quant'),'product_id','tbl_product.name','tbl_product.code','tbl_product.price')    
     ->groupBy('product_id','code','name','price')    
     ->whereDate('tbl_trans.created_at','2017-07-29')  
     ->get();


   return view('reports.productsales',compact('productsales'));