简体   繁体   中英

PHP - How to match today and yesterday date with database field

i have got data in following format in database

  2015-01-16 14:53:02   

i am trying to get record matach with today and another for yesterday date.

here is what i have tried

 //fetch today records
    if ($button1pressed=="T1"){
    $today = date('Y-m-d H:i:s');  
    $sqlquery="WHERE `datetime`="."'".$today."'";
    }

 //fetch yesterday records
    if ($button1pressed=="T2"){
    $yesterday = date('Y-m-d H:i:s',time() - 3600); 
    $sqlquery="WHERE `datetime`="."'".$yesterday."'";
    }

and sql is something like this.

  $sql="SELECT * FROM  `me_olb1` ".$sqlquery ;

please need your help

thanks

That's not going to work, it will only give you results for the exact same hour, minute and second.

You would need to extract the date from the datetime field. For example:

for today:

$sqlquery = " WHERE DATE(`datetime`)=DATE(NOW()) ";

for yesterday:

$sqlquery = " WHERE DATE(`datetime`)=DATE(DATE_SUB(NOW(), INTERVAL 1 DAY)) ";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM