Php and mysql. WHERE date <= X Hours ago?

I was wondering what is the best way to write a where statement in PHP where targetDate <Date.Now is HardCodedHours in PHP

+2


source share


3 answers


This will pull "field1" from table "myTable" where the DATETIME column "targetDate" is older than 12 hours.



$hardcodedHours = 12;
$sql = "SELECT field1 FROM myTable WHERE targetDate <= '" . date('Y-m-d H:i:s', strtotime("-$hardcodedHours hours")) . "'";
$result = mysql_query($sql);

      

+4


source


If you mean how to do it in a MySQL query:



SELECT * FROM table WHERE targetDate <= date_sub(now(), interval 1 hour);

+18


source


$limitTime = time() - $nbHours * 3600;
$query = "SELECT ... WHERE TIMESTAMP(targetDate) < $limitTime;";

      

Or something like that.

0


source







All Articles