• <bdo id='KChqf'></bdo><ul id='KChqf'></ul>
        <legend id='KChqf'><style id='KChqf'><dir id='KChqf'><q id='KChqf'></q></dir></style></legend>

        <tfoot id='KChqf'></tfoot>

        <small id='KChqf'></small><noframes id='KChqf'>

        <i id='KChqf'><tr id='KChqf'><dt id='KChqf'><q id='KChqf'><span id='KChqf'><b id='KChqf'><form id='KChqf'><ins id='KChqf'></ins><ul id='KChqf'></ul><sub id='KChqf'></sub></form><legend id='KChqf'></legend><bdo id='KChqf'><pre id='KChqf'><center id='KChqf'></center></pre></bdo></b><th id='KChqf'></th></span></q></dt></tr></i><div id='KChqf'><tfoot id='KChqf'></tfoot><dl id='KChqf'><fieldset id='KChqf'></fieldset></dl></div>

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

        Laravel Eloquent compare date from datetime field(Laravel Eloquent 比较日期时间字段中的日期)
          <tbody id='1XEeI'></tbody>
        1. <legend id='1XEeI'><style id='1XEeI'><dir id='1XEeI'><q id='1XEeI'></q></dir></style></legend>
          <i id='1XEeI'><tr id='1XEeI'><dt id='1XEeI'><q id='1XEeI'><span id='1XEeI'><b id='1XEeI'><form id='1XEeI'><ins id='1XEeI'></ins><ul id='1XEeI'></ul><sub id='1XEeI'></sub></form><legend id='1XEeI'></legend><bdo id='1XEeI'><pre id='1XEeI'><center id='1XEeI'></center></pre></bdo></b><th id='1XEeI'></th></span></q></dt></tr></i><div id='1XEeI'><tfoot id='1XEeI'></tfoot><dl id='1XEeI'><fieldset id='1XEeI'></fieldset></dl></div>

            <small id='1XEeI'></small><noframes id='1XEeI'>

                <tfoot id='1XEeI'></tfoot>

                • <bdo id='1XEeI'></bdo><ul id='1XEeI'></ul>

                • 本文介绍了Laravel Eloquent 比较日期时间字段中的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

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

                  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
                  

                  但如果我这样做:

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

                  它不会得到该行.

                  我猜这是因为 $date = 2014-07-10 这使得 MySQL 假设它是 2014-07-10 00:00:00.

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

                  在常规 MySQL 中我会这样做

                  In regular MySQL I would just do

                  where DATE(date) <= $date
                  

                  使用 Laravel 的 Eloquent 的等价物是什么?

                  What would be the equivalent using Laravel's Eloquent?

                  推荐答案

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

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

                  他们为您执行 SQL DATE() 工作,并管理 SQLite 的差异.

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

                  你的结果可以这样实现:

                  Your result can be achieved as so:

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

                  更多例子请看#3946的第一条消息和这个Laravel Daily 文章.

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


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


                  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.

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

                  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)
                  

                  注意事项:

                  • 23:59:59 可以(暂时)因为 1 秒的精度,但请看这篇文章:23:59:59 不是一天的结束.不,真的!
                  • 请记住零日期"情况(0000-00-00 00:00:00").尽管应该避免这些零日期",但它们是许多问题的根源.如果需要,最好使该字段可以为空.

                  这篇关于Laravel Eloquent 比较日期时间字段中的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Laravel 4 - Connect to other database(Laravel 4 - 连接到其他数据库)
                  Call external API function from controller, LARAVEL 4(从控制器调用外部 API 函数,LARAVEL 4)
                  Empty string instead of null values Eloquent(空字符串而不是空值 Eloquent)
                  quot;laravel.logquot; could not be opened: failed to open stream(“laravel.log无法打开:无法打开流)
                  Displaying the Error Messages in Laravel after being Redirected from controller(从控制器重定向后在 Laravel 中显示错误消息)
                  Laravel Creating Dynamic Routes to controllers from Mysql database(Laravel 从 Mysql 数据库创建到控制器的动态路由)
                • <legend id='QPG5e'><style id='QPG5e'><dir id='QPG5e'><q id='QPG5e'></q></dir></style></legend>
                • <tfoot id='QPG5e'></tfoot>

                    <small id='QPG5e'></small><noframes id='QPG5e'>

                      <tbody id='QPG5e'></tbody>

                      <i id='QPG5e'><tr id='QPG5e'><dt id='QPG5e'><q id='QPG5e'><span id='QPG5e'><b id='QPG5e'><form id='QPG5e'><ins id='QPG5e'></ins><ul id='QPG5e'></ul><sub id='QPG5e'></sub></form><legend id='QPG5e'></legend><bdo id='QPG5e'><pre id='QPG5e'><center id='QPG5e'></center></pre></bdo></b><th id='QPG5e'></th></span></q></dt></tr></i><div id='QPG5e'><tfoot id='QPG5e'></tfoot><dl id='QPG5e'><fieldset id='QPG5e'></fieldset></dl></div>

                          <bdo id='QPG5e'></bdo><ul id='QPG5e'></ul>