php Laravel ORM,日期比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15053089/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:27:11  来源:igfitidea点击:

Laravel ORM, date compare

phplaravelorm

提问by user2096672

I'm using Laravel Framework.

我正在使用 Laravel 框架。

I want all of the rows for this day. This is what I tried:

我想要这一天的所有行。这是我尝试过的:

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

(field created_atrepresented in database in format: Y-m-d H:i:s).

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

回答by Matt Browne

Hmmm...there was a good answer to this question which seems to have now disappeared.*

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

It was something like this:

它是这样的:

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

Note: if you're putting this code in a file with a namespace, or might use a namespace in the future, you should prefix the DateTime class with a backslash: new \DateTime('today').

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

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

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

回答by Bilal Gultekin

date('Y-m-d H:i:s')returns date of now.

date('Y-m-d H:i:s')现在返回日期。

If you want results from start of the day you can replace it with date('Y-m-d').' 00:00:00'.

如果您想要从一天开始的结果,您可以将其替换为date('Y-m-d').' 00:00:00'.

If you want results from last 24 hours you can replace it with date('Y-m-d H:i:s',time()-86400)(86400 = 24*60*60)

如果您想要过去 24 小时的结果,您可以将其替换为date('Y-m-d H:i:s',time()-86400)(86400 = 24*60*60)

回答by Richard Fu

For those who only need to compare a date without time, Lavarel provided a handy function whereDate:

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

User::whereDate('created_at', '>=', date('Y-m-d'));

http://laraveldaily.com/eloquent-date-filtering-wheredate-and-other-methods/

http://laraveldaily.com/eloquent-date-filtering-wheredate-and-other-methods/

回答by AboElnouR

I know that topic is old but may someone try to find a solution from search engines.

我知道这个话题很旧,但可能有人会尝试从搜索引擎中找到解决方案。

You can use:

您可以使用:

User::whereDate('created_at', '=', Carbon::today()->toDateString());

You can visit this link if you want to know more about date comparison in Laravel using where.

如果您想了解更多关于 Laravel 中使用 where 的日期比较的信息,可以访问此链接。

Eloquent date filtering: whereDate() and other methods

Eloquent 日期过滤:whereDate() 等方法

回答by Usama

Simply:

简单地:

DB::table('users')->where('created_at', '>=', date('Y-m-d'). ' 00:00:00')

When getting all rows for this day(the time should be 00:00:00), so make sure to set the date condition to current date plus 00:00:00 i.e.

当获取这一天的所有行时(时间应该是 00:00:00),所以一定要把日期条件设置为当前日期加上 00:00:00 即

date('Y-m-d'). ' 00:00:00'

回答by PHP Worm...

Use whereDate

whereDate

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

other functions you may use are: whereDate / whereMonth / whereDay / whereYear / whereTime

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