我正在使用Laravel Framework。Laravel ORM,日期比较
我想要这一天的所有行。这是我的尝试:
DB::table('users')->where('created_at', '>=', date('Y-m-d H:i:s'))
(场created_at
数据库格式表示:Y-m-d H:i:s
)。
我正在使用Laravel Framework。Laravel ORM,日期比较
我想要这一天的所有行。这是我的尝试:
DB::table('users')->where('created_at', '>=', date('Y-m-d H:i:s'))
(场created_at
数据库格式表示:Y-m-d H:i:s
)。
date('Y-m-d H:i:s')
返回现在的日期。
如果你想从一天的开始结果,你可以用date('Y-m-d').' 00:00:00'
替换它。
如果你想从过去的24小时的结果,你可以用date('Y-m-d H:i:s',time()-86400)
(86400 = 24 * 60 * 60)替换它
嗯...有一个很好的回答这个问题,这似乎现在已经消失了。 *
它是这样的:
User::where('created_at', '>=', new DateTime('today'))
注意:如果你把这个代码在一个文件与命名空间,或在将来可能会使用一个命名空间,你应该前缀DateTime类反斜杠:new \DateTime('today')
。
* https://stackoverflow.com/questions/15052679/laravel-framework-how-to-get-today-queries
对于那些谁只需要比较日期不受时间,Lavarel提供一个方便的功能whereDate:
User::whereDate('created_at', '>=', date('Y-m-d'));
http://laraveldaily.com/eloquent-date-filtering-wheredate-and-other-methods/
我知道,题目是老但可能有人试图从搜索引擎中找到解决方案。
您可以使用:
User::whereDate('created_at', '=', Carbon::today()->toDateString());
,如果你想用在哪里可以更多地了解Laravel日期比较,您可以访问此链接。
简单:
DB::table('users')->where('created_at', '>=', date('Y-m-d'). ' 00:00:00')
当得到所有行的这一天(具体时间应该是00:00:00),所以一定要在日期条件设置为当前日期加00 :00:00,即
date('Y-m-d'). ' 00:00:00'
虽然此代码片段可能会解决问题,但[包括解释](http://meta.stackexchange.com/questions/114762/explaining-entirely-code- based-answers)确实有助于提高您的质量帖子。请记住,您将来会为读者回答问题,而这些人可能不知道您的代码建议的原因。 – 2018-02-04 21:51:20
的可能的复制[我自己的问题(http://stackoverflow.com/questions/14790027/using-eloquent-how-do-i-filter-my-model-query-只包括项目,那是c),其中雄辩被使用,以及流利。 – 2013-02-24 15:36:19