Laravel 4+提供以下方法:whereDay()
、whereMonth()
、whereYear()
(#3946)和whereDate()
(#6879).
他们为您执行SQL DATE()
,并管理SQLite的差异.
您的结果可以通过以下方式实现:
->whereDate('date', '<=', '2014-07-10')
有关更多示例,请参阅#3946和Laravel Daily article的第一条消息.
Update: Though the above method is convenient, as noted by Arth it is inefficient on large datasets, because the DATE()
SQL function has to be applied on each record, thus discarding the possible index.
以下是进行比较的一些方法(但请阅读下面的注释):
->where('date', '<=', '2014-07-10 23:59:59')
->where('date', '<', '2014-07-11')
// '2014-07-11'
$dayAfter = (new DateTime('2014-07-10'))->modify('+1 day')->format('Y-m-d');
->where('date', '<', $dayAfter)
笔记: