Your Location is: Home > Php

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

From: Senegal View: 1185 handkock 


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 + numOfDays == today. How can I implement this ?

Best answer

As per logic + numOfDays == today

The above condition equals the following  == today - numOfDays

So try the following:

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

CakePHP Condition is: == $compare_date