Question

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

Was it helpful?

Solution

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);

OTHER TIPS

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

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

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

Or something like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top