This question already has an answer here:
As suprising as it may sound, I couldn't find this exact information anywhere else. I need to compare 2 dates with time in a single SQL query. One of the datetimes is the exact end of a promotion and the other one is the datetime of the query (current datetime). My SQL query looks like this now:
$usr_id=$_SESSION['usr_id'];
$now = date("d-m-Y H:i:s");
$sql="SELECT * FROM usr_offers WHERE usr_id='$usr_id' AND of_enddate>='$now'";
This query does not work neither does show any kind of errors - it outputs all data from the table.
So how do I compare two datetime dates with time in them in a SQL query?
</div>
The default datetime format of mysql is "Y-m-d H:i:s" Change your datetime format to
$now = date("Y-m-d H:i:s");
and try again.
Check your date format stored in the database. If it is stored as date time then change the format as
$now = date('Y-m-d H:i:s');
or it may stored as time string then convert the current time into the string and then compare the date.