php Laravel Eloquent 比较日期时间字段中的日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25139948/
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 17:41:17  来源:igfitidea点击:

Laravel Eloquent compare date from datetime field

phpmysqldatetimelaravel-4

提问by almo

I want to get all the rows from a table through an expression:

我想通过表达式从表中获取所有行:

table.date <= 2014-07-10

But if the column contains a datetime let's say:

但是,如果该列包含日期时间,让我们说:

2014-07-10 12:00:00

But if I do:

但如果我这样做:

where('date', '<=', $date)

it won't get the row.

它不会得到行。

I guess this is because $date = 2014-07-10 which makes MySQL assume that it is 2014-07-10 00:00:00.

我猜这是因为 $date = 2014-07-10 这让 MySQL 假设它是 2014-07-10 00:00:00。

In regular MySQL I would just do

在常规 MySQL 中,我会做

where DATE(date) <= $date

What would be the equivalent using Laravel's Eloquent?

使用 Laravel 的 Eloquent 等效于什么?

回答by Gras Double

Laravel 4+ offers you these methods: whereDay(), whereMonth(), whereYear()(#3946) and whereDate()(#6879).

Laravel 4+ 为您提供了这些方法:whereDay(), whereMonth(), whereYear()( #3946) 和whereDate()( #6879)。

They do the SQL DATE()work for you, and manage the differences of SQLite.

他们DATE()为您完成 SQL工作,并管理 SQLite 的差异。

Your result can be achieved as so:

你的结果可以这样实现:

->whereDate('date', '<=', '2014-07-10')

For more examples, see first message of #3946and this Laravel Daily article.

有关更多示例,请参阅#3946 的第一条消息和这篇Laravel Daily 文章


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.


更新:虽然上述方法很方便,但正如 Arth 指出的那样,它在大型数据集上效率低下,因为DATE()必须在每条记录上应用 SQL 函数,从而丢弃可能的索引。

Here are some ways to make the comparison (but please read notes below):

以下是进行比较的一些方法(但请阅读下面的注释):

->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)

Notes:

笔记:

  • 23:59:59 is okay (for now) because of the 1-second precision, but have a look at this article: 23:59:59 is not the end of the day. No, really!
  • Keep in mind the "zero date" case ("0000-00-00 00:00:00"). Though, these "zero dates" should be avoided, they are source of so many problems. Better make the field nullable if needed.
  • 23:59:59 可以(暂时)因为 1 秒的精度,但是看看这篇文章:23: 59: 59 不是一天的结束。不完全是!
  • 请记住“零日期”情况(“0000-00-00 00:00:00”)。虽然应该避免这些“零日期”,但它们是许多问题的根源。如果需要,最好使该字段可以为空。

回答by Arth

Have you considered using:

您是否考虑过使用:

where('date', '<', '2014-08-11')

You should avoid using the DATE()function on indexed columns in MySQL, as this prevents the engine from using the index.

您应该避免DATE()在 MySQL 中的索引列上使用该函数,因为这会阻止引擎使用索引。

UPDATE

更新

As there seems to be some disagreement about the importance of DATE()and indexes, I have created a fiddlethat demonstrates the difference, see POSSIBLE KEYS.

由于似乎对DATE()和 索引的重要性存在一些分歧,因此我创建了一个演示差异的小提琴,请参阅POSSIBLE KEYS

回答by Majbah Habib

You can get the all record of the date '2016-07-14' by using it

您可以使用它获取日期“2016-07-14”的所有记录

 whereDate('date','=','2016-07-14')

Or use the another code for dynamic date

或者使用另一个代码作为动态日期

whereDate('date',$date)

回答by Jakir Hossain

You can use this

你可以用这个

whereDate('date', '=', $date)

If you give whereDate then compare only date from datetime field.

如果您提供 whereDate,则仅比较日期时间字段中的日期。

回答by pixie_frigo

If you're still wondering how to solve it.

如果您仍然想知道如何解决它。

I use

我用

$protected $dates = ['created_at','updated_at','aired'];

$protected $dates = ['created_at','updated_at','aired'];

In my model and in my where i do

在我的模型和我做的地方

where('aired','>=',time())

where('aired','>=',time())

So just use the unix to compaire in where.

所以只需使用unix来比较在哪里。

In views on the otherhand you have to use the date object.

另一方面,在视图中,您必须使用日期对象。

Hope it helps someone!

希望它可以帮助某人!