【问题标题】:Object of class Carbon\Carbon could not be converted to intCarbon\Carbon 类的对象无法转换为 int
【发布时间】:2017-05-18 05:04:48
【问题描述】:

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

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

$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"
}

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

希望大家指点和建议

【问题讨论】:

  • 您要寻找的价值是什么?到目前为止,您尝试了什么?
  • 我正在寻找值“2017-01-04 00:35:38” - 我尝试过类似 $user->updated_at->date 和 $user->updated_at(date) 但所有其中没有给我任何东西

标签: php mysql laravel laravel-5.2


【解决方案1】:

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

$user->updated_at->toDateTimeString();

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

【讨论】:

    【解决方案2】:

    在使用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-19
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 2017-03-08
      • 2019-01-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多