Your Location is: Home > Php

CakePHP: Adding days to a Date field and compare them in find query

From: Senegal View: 1185 handkock 

Question

I have a Model in my database, which has a Datetime field. I have to write a find query in CakePHP like $this->Model->find('....., where I add a number of days to this Datetime and then I compare to today's date. Something like Model.date + numOfDays == today. How can I implement this ?

Best answer

As per logic

Model.date + numOfDays == today

The above condition equals the following

Model.date  == today - numOfDays

So try the following:

$numOfDays = -1 * $numOfDays;
$compare_date = date('Y-m-d H:i:s', strtotime($numOfDays +' days'));

CakePHP Condition is:

Model.date == $compare_date