Agnosco
Agnosco

Reputation: 155

check If time difference between date in database greater than today

I have 'startdate' column in format of 'm-d-y' and 'noofdays' column in a table. I have to find if the no of days greater than days difference between today and start date. I have to fetch data using the query. How can i do it?I tried like this.

$this->db->select('startdate');
$this->db->from('db_dates');
$sdate = date("Y-m-d", strtotime(str_replace('-', '/', `startdate`)));
$timediff = abs(strtotime($sdate) - time());
$days     = floor($timediff/(60*60*24));
$where = "( noofdays = 0 OR ( noofdays >='".$days))";
$this->db->where($where);

Upvotes: 0

Views: 569

Answers (1)

Marcus Adams
Marcus Adams

Reputation: 53830

I suppose since you put MySQL tag, you really want MySQL solution, not PHP:

SELECT startdate
FROM db_dates
WHERE ABS(DATEDIFF(STR_TO_DATE(startdate, '%m-%d-%Y'), CURDATE())) > noofdays

You might need to adjust for time zones, but this should get you started.

Upvotes: 1

Related Questions