使用datetime而不是mysql now()?

时间:2016-07-17 18:55:32

标签: php

我有这个问题:

$stmt = $mysqli->prepare("

SELECT id, titulo
FROM posts WHERE 
data < DATE_SUB( NOW( ) , INTERVAL 140 MINUTE ) 
AND
CURDATE() = DATE(data)
ORDER BY RAND( ) limit 1

");
 $stmt->execute();

而不是now()我想使用PHP日期时间,任何想法如何使用它?

1 个答案:

答案 0 :(得分:1)

是的,您可以将PHP变量作为绑定参数注入SQL语句:

$stmt = $mysqli->prepare("
  SELECT id, titulo
  FROM posts WHERE 
  data < DATE_SUB( ? , INTERVAL 140 MINUTE ) 
  AND DATE(data) = DATE(?)
  ORDER BY RAND( ) LIMIT 1");
$curdatetime = date("Y-m-d H:i:s");
$stmt->bind_param("ss", $curdatetime, $curdatetime);
$stmt->execute();