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
2009-09-17 16:56:21
+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
2009-09-17 16:57:25
A:
$limitTime = time() - $nbHours * 3600;
$query = "SELECT ... WHERE TIMESTAMP(targetDate) < $limitTime;";
Or something like that.
p4bl0
2009-09-17 16:57:43