Home » Php » php – STR_TO_DATE not working in my query

php – STR_TO_DATE not working in my query

Posted by: admin July 12, 2020 Leave a comment


I have date field in database having varchar datatype. Now I have to compare date with the current date but because of data type(varchar) result is not coming properly.

I don’t want to change datatype in database so how can I query in codeigniter ?

In my database date is in this format 30/11/2015

My current query:

    //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.

But proper result is not coming….

How to&Answers:

Try this may be it will helps you :

$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');

You need to change date format if you use date_format in your query you are changing in same format may be that is the reason it is not working. try to change and compare in Y-m-d format.


**Hope this may help you...**

$cd = date('d/m/Y');

$data['project'] = $this->common_model->selectRecord("SELECT * from project where project_date = '.$cd.'");

 in my project this is working ..


Can you check this query manually. Then try to add it in CodeIgniter

$cd = date('d/m/Y');//current date

select * FROM warranty WHERE DATE_FORMAT(STR_TO_DATE(vd, '%d/%m/%Y'), '%Y-%m-%d') > DATE_FORMAT(STR_TO_DATE($cd, '%d/%m/%Y'), '%Y-%m-%d')


Try this:

$cd = date('d.m.Y');//current date

$this->db->where("STR_TO_DATE(vd, '%d.%m.%Y') >",$cd);
$query =$this->db->get('warranty');

To be sure that it comes out in the format you desire, use DATE_FORMAT:

$cd = date('d.m.Y');//current date

$this->db->where("DATE_FORMAT(STR_TO_DATE(vd, '%d.%m.%Y'),'%d.%m.%Y') >",$cd);
$query =$this->db->get('warranty');


Have you tried CAST ?

SELECT * FROM warranty WHERE CAST(vd AS DATE) ....;

Have you tried DATE ?

SELECT * FROM warranty WHERE DATE(vd) ....;


Change this

$this->db->where("date_format(STR_TO_DATE(vd, '%d/%m/%Y'),'%d/%m/%Y') >",$cd);


$this->db->where("STR_TO_DATE(vd, '%d/%m/%Y') > NOW()");


In your code you are using two function to restructure the date format.

  1. date_format


$now = date('d/m/Y'); //current date
$userdate = '2009-05-18 22:23:00'; //which get from user inputs

$dateModified = STR_TO_DATE($userdate,'%d/%m/%Y'); //output is 2009-05-18

So in function

$this->db->where('order_date >=', $dateModified);
$this->db->where('order_date <=', $now);
$query =$this->db->get('warranty');


$this->db->where('order_date BETWEEN '.$dateModified.' and '.$now);

This function will give you result of userSearch input and date to now. This function simply act like between in MySQL

enter image description here