How to compare DateTime Column with Only Date not time

I am using mySql and the table column dateis of type DATETIME

But I want to compare only today the date is not the time

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

IF I am comparing a date using the method above, it is always 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'm not a good mySQL programmer.

+5
source share
3 answers

there is a mysql function called DATE that retrieves the date part of a date or date and time expression

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

edit: be careful, as stated in the comments below, using this bypasses the index in the datetime field. Instead, you should use:

AND date BETWEEN '$today 00:00:00' AND '$today 23:59:59'
+18
source

You can use the datediff function as follows:

AND datediff(`date`, '$today') = 0
+1
source
date_format('%Y-%m', time()) 

, , .

0

All Articles