0

My requirement is to pull records from mysql database which have just 5 mins left from the current time as per the one of the columns in the database. The column is has user inserted datetime.

     date_default_timezone_set("UTC");
  $utc_time = date("Y-m-d H:i:s", time());
 echo "UTC Time: " . $utc_time . "<br>";


$result = mysql_query("select reminder_text, reminder_subject, reminder_date_time_utc  $table_name where (TIME_TO_SEC (TIMEDIFF(reminder_date_time_utc , '$utc_time')) < 300) AND (TIMEDIFF(reminder_date_time_utc , '$utc_time')) > 0) ") or die(mysql_error());

here the reminder_date_time inside the TIMEDIFF function is the column name to pick up the DATETIME. Using this query I do not get the results but if I place the date instead of reminder_date_time it gives me the correct output. For example if I say TIMEDIFF('2013-07-12 11:05:00' , '$utc_time') it gives me the correct output. And this same value: 2013-07-12 11:05:00 is actually present in one of the rows of this column reminder_date_time_utc

Any advice where I am going wrong... Does TIMEDIFF function not accept column name as one of the parameters.

2 Answers2

0

Do you forgot the FROM in your sql? Why dont you try to do it like following:

$utc_time = date("Y-m-d H:i:s", time() - 30000);

and change the query to

'SELECT * FROM ' . $tablename . ' WHERE reminder_date_time_utc > "' . $utc_time . '"';
DaKirsche
  • 352
  • 1
  • 14
0

or use DATE_ADD() function :

'SELECT * FROM ' . $tablename . ' WHERE reminder_date_time_utc > DATE_ADD(NOW(), INTERVAL -'5' SECOND));

jaczes
  • 1,366
  • 2
  • 8
  • 16