【问题标题】:codeigniter : getting data between two dates in mysql using phpcodeigniter:使用 php 在 mysql 中获取两个日期之间的数据
【发布时间】:2015-06-16 23:02:51
【问题描述】:

我想在mysql中检索两个日期之间的数据。

从日期:01/04/2015 迄今为止:01/05/2015

但我无法在单个变量中获取这些日期;

我是如何得到如下所述的:

$fdate=01;
$fmonth=04;
$tdate=01;
$tmonth=05;
$year=2015;

我在模型中的代码:

function date_range($fdate,$fmonth,$tdate,$tmonth,$year)
        {
            $this->db->select('*');
            $this->db->where('DAY(order_date) >=',$fdate);
            $this->db->where('MONTH(order_date) >=',$fmonth);
            $this->db->where('YEAR(order_date) >=',$year);
            $this->db->where('DAY(order_date) <=',$tdate);
            $this->db->where('MONTH(order_date) <=',$tmonth);
            $this->db->where('YEAR(order_date) <=',$year);
            return $this->db->get('orders');
        }

有时它会正确返回结果,有时它不会返回结果,但我在提到的两个日期之间在mysql 中有一个数据。

我在这段代码上犯了什么错误,请帮助我

【问题讨论】:

标签: php mysql codeigniter


【解决方案1】:
public function get_between_dates($date_from, $date_to)
{
    $this->db->where('order_date >=', $date_from);
    $this->db->where('order_date <=', $date_to);
    return $this->db->get()->result();
}

这是获取两个日期之间数据的方法。将表单输入分别命名为 date_from 和 date_to。

【讨论】:

    【解决方案2】:

    你可以使用

    $this->db->where("YOUR COLUMN BETWEEN DATE_FORMAT(COLUMN1,'%d/%m/%Y') AND DATE_FORMAT(COLUMN2,'%d/%m/%Y'"));
    

    【讨论】:

      【解决方案3】:

      你可以使用 CodeIgniter:

      $query = $this->db->query("YOUR QUERY");
      

      你的"Your QUERY" 将包含mysql DATE_FORMAT(NOW(),'%b %d %Y %h:%i %p')

      例如使用Query Binding

      sql = "SELECT * FROM some_table 
             WHERE order_date between  DATE_FORMAT(? ? ?,'%d %m %Y') 
                                   AND DATE_FORMAT(? ? ?,'%d %m %Y')"; 
      
      $this->db->query($sql, array($fdate, $fmonth, year, $tdate, $tmonth, year));
      

      【讨论】:

      • 查尔斯利亚姆!我不能正确理解请再举一个例子
      【解决方案4】:

      应该这样做:

      function date_range($fdate,$fmonth,$tdate,$tmonth,$year)
              {
                  $this->db->select('*');
                  $this->db->where('order_date <=',date('Y-m-d',mktime(0,0,0,$fmonth,$fdate,$year)))
                  $this->db->where('order_date >=',date('Y-m-d',mktime(0,0,0,$tmonth,$tdate,$year)))
                  return $this->db->get('orders');
              }
      

      【讨论】:

      • 在where 条件下,如果将default date 设为1970-01-01,则无法获得给定日期
      猜你喜欢
      • 2020-07-25
      • 1970-01-01
      • 1970-01-01
      • 2011-10-12
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 2021-06-01
      • 1970-01-01
      相关资源
      最近更新 更多