Comparing Today Date in MySQL

Conventionally getting today date from database in the past would require the use of date('Y-m-d'); function and add it to MySQL query such as below:

$today_date = date('Y-m-d');
$tmrw_date = date('Y-m-d', strtotime("+1 day"));
$sql = "SELECT * FROM my_table
    WHERE my_datetime BETWEEN '" . $today_date . "' AND '" . $tmrw_date . "'";

However this method might return false positive result if the web server and database server doesn’t have it date and time synchronized. From my point of view, it best to let the database server have the full control to insert, update or compare the dates. So what the alternative? You can check the full specification from MySQL Date and Time Functions but I would opt for DATEDIFF if you specifically want to compare with today date.

SELECT * FROM my_table
    WHERE DATEDIFF(my_datetime, SYSDATE())=0
Posted in Best Practices, MySQL, Tutorial | 2 Comments

2 Responses to Comparing Today Date in MySQL

  1. hanep says:

    ok. it means the time is now fully synchronized to the server time. kan? hish, da start pening..

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>