Laravel ORM,日期比较


Laravel ORM, date compare

我使用的是Laravel框架。

我要这一天所有的排。这就是我尝试的:

DB::table('users')->where('created_at', '>=', date('Y-m-d H:i:s'))

(字段created_at在数据库中以格式表示:Y-m-d H:i:s)。

嗯。。。这个问题有一个很好的答案,但现在似乎已经消失了。*

它是这样的:

User::where('created_at', '>=', new DateTime('today'))

注意:如果您将此代码放在具有名称空间的文件中,或者将来可能使用名称空间,则应该在DateTime类前面加一个反斜杠:new 'DateTime('today')

*https://stackoverflow.com/questions/15052679/laravel-framework-how-to-get-today-queries

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)

对于那些只需要比较日期而不需要时间的人,Lavarel提供了一个方便的函数其中日期

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中使用where进行日期比较的信息,可以访问此链接。

Eloquent日期过滤:whereDate()和其他方法

简单地说:

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'

使用whereDate

$users = DB::table('users')
                ->whereDate('created_at', '2016-12-31')
                ->get();

您可以使用的其他功能有:whereDate / whereMonth / whereDay / whereYear / whereTime