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

时间:2017-01-04 01:18:01

标签: php mysql laravel laravel-5.2

我已经搜索过了,我确实找到了一些讨论这个问题的话题,但是我觉得这些都不是我想去的方式,因此我希望能在这里得到更好的消化。

我希望从用户上次更新时获得时间戳。

$user->updated_at

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

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

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

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

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

希望得到一些指示和建议

2 个答案:

答案 0 :(得分:12)

Carbon对象可以使用toDateTimeString方法:

$user->updated_at->toDateTimeString();

它会为您提供值2017-01-04 00:35:38作为字符串。

答案 1 :(得分:0)

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

示例:

现在您在做什么。

dd($user->updated_at);

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

建议:

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

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