how to compare DateTime Column with Only Date not time

19,297

Solution 1

there is a mysql function called DATE which extract the date part of a date or datetime expression

AND DATE(`date`)='$today'

edit: be carefull, as said in the comments below, using this will bypass index on datetime field. You should use instead :

AND date BETWEEN '$today 00:00:00' AND '$today 23:59:59'

Solution 2

You can use the datediff function like this:

AND datediff(`date`, '$today') = 0
Share:
19,297
Admin
Author by

Admin

Updated on June 18, 2022

Comments

  • Admin
    Admin almost 2 years

    I am using mySql and my table column date is DATETIME type

    But i want to compare only today's date not time

           $today = date("Y-m-d",  time());
    

    IF i compare date using above method it always come wrong.

    $this->update("
    UPDATE `stats_tracker` 
    SET 
      `user_agent`='$this->visitor_user_agent' , 
      `referrer`='$this->referrer_page' , 
      `page_views`='$page_views' 
    WHERE 
      `visitor_ip`='$this->visitor_ip' 
      AND `page`='$this->this_page' 
      AND `date`='$today'");
    

    Please Help me i am not a good mySQL programmer.