我正在尝试检查现在的时间是否比 SQL 行中的 DATETIME 新。这是我当前的代码:
<?php
require('common.php');
$query = "SELECT
event_name, UNIX_TIMESTAMP(event_date) AS event_time, initiator, min_level, max_level
FROM
DD_events
WHERE
event_date <= NOW()
";
try{
$stmt = $db->prepare($query);
$result = $stmt->execute();
}
catch(PDOException $ex){
die("Failed to run query: " . $ex->getMessage());
}
$chk = $stmt->fetchall();
//Loop through each row
foreach ($chk as $chks) {
print_r($chks['event_time']); echo "<br>";
// If it is, create query to make the next event
$query = "INSERT INTO
DD_events (event_name, event_date, initiator, min_level, max_level)
VALUES
(:name, :event_date, 0, :min_lvl, :max_lvl)
";
$query_params = array(
':name' => $chks['event_name'],
':event_date' => $chks['event_time'],
':min_lvl' => $chks['min_level'],
':max_lvl' => $chks['max_level']
);
try
{
// These two statements run the query against your database table.
$stmt = $db->prepare($query);
$stmt->execute($query_params);
}
catch(PDOException $ex)
{
// Note: On a production website, you should not output $ex->getMessage().
// It may provide an attacker with helpful information about your code.
die("Failed to run query: " . $ex->getMessage());
}
}
结果总是空的,即使我知道应该会出现。
我的目标是检查一个事件是否已经过去。如果它已经执行代码。
我知道 PHP 时间与 SQL 时间不同,这就是为什么我尝试使用 UNIX_TIMESTAMP() 函数来尝试将其与 PHP 时间进行比较。