问题描述:

I want to filter my results using PHP to display only data from a table that was inserted TODAY. The format is as follows:

column date in table daily in the format 2013-01-06 17:36:11. How would I filter queries using PHP to only display queries on todays date?

网友答案:
WHERE DATE(`date`) = DATE(NOW())
网友答案:

It's MUCH better practice to do in the where clause of your sql query. to expand upon Madara Uchiha's answer: if you're using MySQL, your query will look something like:

SELECT * FROM daily WHERE DATE(`date`) = DATE(NOW()).

If you must do it in PHP for whatever odd reason, which I don't recommend, you'll have to loop the results of your query and compare the date field of each database row with today's date in PHP.

//setup the array our filtered results will go in
$filteredResults = array();

//get the timestamp for 12:00:00am and 11:59:59pm today 
$greaterThanThisTime = mktime(0,0,0,date('n'),date('j'),date('Y'));
$lessThanThisTime = mktime(23,59,59,date('n'),date('j'),date('Y'));

//loop the database results
foreach($dbResults as $i=>$row) {
  $rowTimeStamp = strtotime($row['date']);
  if($rowTimeStamp>=$greaterThanThisTime && $rowTimeStamp<=$lessThanThisTime) {
    $filteredResults[] = $row;
  }
}

//continue to do whatever you need, using $filteredResults as your results
相关阅读:
Top