我在具有 varchar 数据类型的数据库中有日期字段。现在我必须将日期与当前日期进行比较,但由于数据类型 (varchar) 结果无法正常显示。
我不想更改数据库中的数据类型,那么如何在 codeigniter 中查询?
在我的数据库中,日期是这种格式 30/11/2015
我当前的查询:
//here vd is table field(one column).
$cd = date('d/m/Y');//current date
$this->db->where("date_format(STR_TO_DATE(vd, '%d/%m/%Y'),'%d/%m/%Y') >",$cd); //comparing date with current date
$query =$this->db->get('warranty');
By using above query it is just comparing the date not month and year. So it is returning those record which date is greater than today's date.
但正确的结果并没有到来....
最佳答案
试试这个可能对你有帮助:
$cd = date('Y-m-d');
$this->db->where("date_format(STR_TO_DATE(vd, '%d/%m/%Y'),'%Y-%m-%d') >",$cd);
$query =$this->db->get('warranty');
如果您在查询中使用 date_format,则需要更改日期格式,您正在以相同的格式进行更改,这可能是它不起作用的原因。尝试以 Y-m-d 格式进行更改和比较。
关于php - STR_TO_DATE 在我的查询中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33968524/