laravel 类 Carbon\Carbon 的对象无法转换为 int

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

Object of class Carbon\Carbon could not be converted to int

phpmysqllaravellaravel-5.2

提问by Patrick

I have already search for this, and i did find some topics discussing this problem, however none of which I felt like was the way I want to go, so therefore I hope to get a better sugestion here.

我已经搜索过这个,我确实找到了一些讨论这个问题的话题,但是我觉得没有一个是我想要走的路,所以我希望在这里得到更好的建议。

I want to get a timestamp from when a user was last updated.

我想从用户上次更新时获取时间戳。

$user->updated_at

If i run this, it gives me this error

如果我运行它,它会给我这个错误

Object of class Carbon\Carbon could not be converted to int

This is because it returns a Carbon instance in Laravel 5.2, and I only need the timestamp.

这是因为它在 Laravel 5.2 中返回一个 Carbon 实例,而我只需要时间戳。

Carbon {#303 ▼
  +"date": "2017-01-04 00:35:38.000000"
  +"timezone_type": 3
  +"timezone": "UTC"
}

How do i fetch the "date" from the carbon instance, so I only get 2017-01-04 00:35:38- I have no idea where .000000 comes from, as this doesn't show up in the database. In the MySQL database, it simply shows 2016-07-20 20:23:07as of type timestamp.

我如何从碳实例中获取“日期”,所以我只能得到2017-01-04 00:35:38- 我不知道 .000000 来自哪里,因为它没有出现在数据库中。在 MySQL 数据库中,它仅显示2016-07-20 20:23:07为类型timestamp

Hope for some pointers and suggestions

希望得到一些指点和建议

回答by Dekel

The Carbonobject has a toDateTimeStringmethod you can use:

Carbon对象有一个toDateTimeString您可以使用的方法:

$user->updated_at->toDateTimeString();

It will give you the value 2017-01-04 00:35:38as a string.

它会给你2017-01-04 00:35:38一个字符串的值。

回答by Shaan Ansari

While using Carbon library to perform multiple action using date.

使用 Carbon 库使用日期执行多个操作时。

Example :

例子 :

Right now what you are doing.

现在你在做什么。

dd($user->updated_at);

output : Carbon {#724 ▼
                  +"date": "2018-06-20 18:28:48.000000"
                  +"timezone_type": 3
                  +"timezone": "UTC"
                }

Suggest :

建议 :

 dd($user->updated_at->toDateTimeString());

 output : 2018-06-20 18:28:48