views:

211

answers:

3

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

+3  A: 

This will pull "field1" from table "myTable" where a 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);
pix0r
+6  A: 

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

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

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

Or something like that.

p4bl0