php - 获取结果时仅考虑日期的前两个值

标签 php mysql codeigniter date

在这里,我想根据我想提供的日期获取结果,但返回整个结果,数据库看起来像这样

数据库看起来像这样

customer_account_id     customer_id     date           code     
    11                    55         01-01-2017     ALI HAJI        
    12                    55         02-01-2017     ALI HAJI                
    13                    55         03-01-2017     ALI HAJI                
    125                   65         01-02-2017     SHARFU              
    126                   55         02-02-2017     ALI HAJI        
    127                   55         03-02-2017     ALI HAJI            
    128                   55         31-01-2017     ALI HAJI

Controller 看起来像这样

 public function report()
{
    $data['result']= $this->Account_model->get_report($_POST['from_date'],$to1['to_date'])->result();
    $this->load->view('report_payment_details',$data);

}

我的模型是这样的

public function get_report($from1,$to1)
{

$from=date('d-m-Y',strtotime($from1));
var_dump($from);
$to=date('d-m-Y',strtotime($to1));
var_dump($to);  
$this->db->order_by('customer_account_id','desc');
$this->db->where('date >=', $from);
$this->db->where('date <=', $to);       
return $this->db->get('customer_accounts');

}

在这里,如果我选择 01-01-2017 和 03-01-2017 之间的日期,那么也会显示整个结果。问题是只有前两个值是从日期开始考虑的,例如,如果我考虑 01- 01-2017这里只有01在考虑,一直没考虑,请帮我解决一下

最佳答案

如果您想坚持使用 VARCHAR,也许您可​​以尝试使用 STR_TO_DATE

$from=date('d-m-Y',strtotime($from1));
var_dump($from);
$to=date('d-m-Y',strtotime($to1));
var_dump($to);  
$this->db->order_by('customer_account_id','desc');
$this->db->where("STR_TO_DATE(`date`,'%d-%m-%Y') >=", "STR_TO_DATE('".$from."','%d-%m-%Y')");
$this->db->where("STR_TO_DATE(`date`,'%d-%m-%Y') <=", "STR_TO_DATE('".$to."','%d-%m-%Y')"); 
return $this->db->get('customer_accounts');

关于php - 获取结果时仅考虑日期的前两个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43363977/

相关文章:

php - 我怎么知道一个 php 文件是否只从客户端的 js 调用?

php - 从 Codeigniter 执行 Python 文件

php - Laravel 4 REST API 连接表

php - CodeIgniter 无法在本地主机上工作

php - 将 Bootstrap 集成到 codeigniter

php - 即使插入查询失败/设计问题,自动增量主键也会增加?

php - 自动刷新网页

mysql - 选择查询具有父子关系的类别数据

java - 使用一组 pdf 文件创建一个 Zip 文件

php - codeigniter - 在 Controller 中使用助手不起作用