【问题标题】:Laravel - where less/greater than date syntaxLaravel - 小于/大于日期语法
【发布时间】:2017-05-02 00:35:10
【问题描述】:

这没有显示正确的计数。正确的语法是什么?

$this->data['Tasks'] = \DB::table('tb_tasks')->where('Status', 'like', 'Open%')->whereDate('DeadLine', '>', 'CURDATE()')->count();

【问题讨论】:

标签: php laravel


【解决方案1】:

使用 Carbon 实例:

$this->data['Tasks'] = \DB::table('tb_tasks')->where('Status', 'like', 'Open%')->whereDate('DeadLine', '>', Carbon::now())->count();

【讨论】:

    【解决方案2】:

    使用DB::raw:

    ->where('datefield', '>', \DB::raw('NOW()'))
    

    【讨论】:

      【解决方案3】:

      我们也可以试试这个。它对我有用。

          $date = "2020-04-10";
      
          /* 
          Assumimng DB `login_date` datetime format is "Y-m-d H:i:s"
          */
      
          $from_date = $date.' 00:00:01';
      
          ->where('login_date', '>=', $from_date);
      

      通过在查询中添加 Where 子句,我们可以找到具有 特定日期之后的行。

      选项 2:

      $date = "2020-03-25";    // Format: date('Y-m-d);
      
      $orders = DB::table('orders')
                     ->select('*')
                     ->whereDate('order_datetime', '<=', $date)
                     ->get();
      
      // Here, Table Field "order_datetime", type is "datetime" 
      // Assuming DB `order_datetime` stores value format like: "Y-m-d H:i:s"
      

      【讨论】:

      • 如有任何疑问或建议请评论。
      • 请更新答案以确保它具有来自问题的相关内容(也许修改您的实现提供的代码?)并且没有解释变量$from_date的使用,这使得它很难理解。
      • 当日期存储在变量中时,不需要添加'或"来括起日期
      • 当我比较两边的日期时间时我应该怎么做
      • @dhruvinprajapati 您可以添加 whereBetweenwhere('reservation_from', '&gt;=', $now)-&gt;where('reservation_from', '&lt;=', $to)
      【解决方案4】:

      你可以像下面这样使用whereDate

      $query->whereDate('DeadLine', '>', Carbon::now())->count();
      

      【讨论】:

        猜你喜欢
        • 2018-04-30
        • 2022-11-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-31
        • 1970-01-01
        • 2019-07-08
        • 2019-02-09
        相关资源
        最近更新 更多