【发布时间】:2016-09-14 08:45:24
【问题描述】:
我的 MySQL 数据库中有一个日期时间字段。我正在使用 Laravel,并且已经包含了 Carbon 库。我正在构建一个函数来将此日期时间字段与当前日期进行比较。目前,在 Carbon 中使用 diffInDays 函数时,结果如下:
Datetime field | current time | result
2016-09-13 23:59:59 | 2016-09-14 16:42:12 | 0
2016-09-13 12:05:59 | 2016-09-14 16:42:12 | 1
2016-09-12 23:59:59 | 2016-09-14 16:42:12 | 1
2016-09-12 12:05:59 | 2016-09-14 16:42:12 | 2
但是,我只想比较日期,所以我希望结果是:
Datetime field | current time | result
2016-09-13 23:59:59 | 2016-09-14 16:42:12 | 1
2016-09-13 12:59:59 | 2016-09-14 16:42:12 | 1
2016-09-12 23:59:59 | 2016-09-14 16:42:12 | 2
2016-09-12 12:05:59 | 2016-09-14 16:42:12 | 2
PHP/Carbon/其他库中是否已经为此预建了函数?
【问题讨论】:
-
日期时间有diff() method;
-
并且它返回的 DateInterval 类型有一个
days属性。 -
Carbon 扩展了 DateTime,因此就像马克说的那样,您可以使用
diff()方法 -
阅读问题。使用你建议的方法会导致上面提到的我不想要的。
-
将每个操作数的时间设置为 00:00:00? 耸耸肩
标签: php mysql laravel datetime php-carbon