I am trying to check the time right now is newer then a DATETIME from a SQL row. This is my current code:
<?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());
}
}
The results always turn up empty even though I know that there are supposed to be turning up.
My goal is to check to see if an event has passed. If it has execute the code.
I understand that PHP time differes from SQL time and that is why I attempted the UNIX_TIMESTAMP() function to try and compare it to the PHP time.
An alternative would be to just select the rows that you need (if you are not going to use the other ones in your code):
That would reduce your php code and the resources needed.