【问题标题】:Carbon subtracting Datetime from Carbon::now()Carbon 从 Carbon::now() 中减去 Datetime
【发布时间】:2014-07-13 23:24:36
【问题描述】:

我想使用这个 Carbon 函数: Carbon::now()->subDays(5)->diffForHumans()

我需要创建正确的整数。

我正在加载一个带有日期时间的字符串,我想在 Laravel 中像这样减去它:

 $datetime = $score->created_at;

然后我将当前时间保存到一个变量中

 $now = Carbon::now()->toDateTimeString();

这是我得到的:

 echo $now . '<br>'; // 2014-07-13 22:53:03
 echo $datetime;     // 2014-07-12 14:32:17

但是当我想从另一个中减去一个时,我得到以下错误:

 echo $now - $datetime;

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

我们将不胜感激。

【问题讨论】:

    标签: php datetime


    【解决方案1】:

    我知道这有点晚了,但这有效:

    $score->created_at->diffForHumans(\Carbon\Carbon::now())
    

    【讨论】:

      【解决方案2】:

      如果要更改日期格式,只需使用格式功能

      $now = Carbon::now();
      
      $score->created_at->diffForHumans($now)->format('Y-m-d');
      

      【讨论】:

        猜你喜欢
        • 2017-04-28
        • 2017-04-01
        • 2017-04-08
        • 2015-06-10
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        • 2011-05-15
        • 2015-10-01
        相关资源
        最近更新 更多