简体   繁体   English

如何在html表(php)中从Mysql表显示特定时间段(天/周/月)的数据

[英]How to display data for a certain period of time (day / week / month) from Mysql table in html table (php)

I use this code to display data from MySQL table in html, but I need the ability to create html tables that will display records added in the last day / week / month. 我使用此代码以html格式显示来自MySQL表的数据,但我需要能够创建html表格以显示最后一天/一周/一个月中添加的记录的功能。

Here is what I have so far: 这是我到目前为止的内容:

<?php            
$db_host = 'localhost';         
$db_name = 'DB';         
$db_username = 'NAME';         
$db_password = 'PASS';       
$db_table_to_show = 'TABLE';         
$connect_to_db = mysql_connect($db_host, $db_username, $db_password) 
or die("Could not connect: " . mysql_error());           
mysql_select_db($db_name, $connect_to_db)      
or die("Could not select DB: " . mysql_error());   
mysql_set_charset("utf8");          
$qr_result = mysql_query("select * from " . $db_table_to_show)      
or die(mysql_error());            
echo '<table id="table">';       
echo '<thead>';         
echo '<tr>';
echo '<th class="table-fandom">fandom</th>';        
echo '<th>author</th>';     
echo '<th class="table-name">name</th>';
echo '<th>size</th>';    
echo '<th class="table-status">status</th>';             
echo '<th>date</th>';       
echo '<th>tags</th>';
echo '</tr>';       
echo '</thead>';        
echo '<tbody>';         

while($data = mysql_fetch_array($qr_result)){       
   echo '<tr>';
   echo '<td>' . $data['fandom'] . '</td>';    
   echo '<td>' . $data['author'] . '</td>';         
   echo '<td><a href="/goto/' . $data['url'] . '" target="_blank">' . $data['name'] . '</a><div class="menu" style="display: none;"><br> '                                             . $data['annotation'] .  '  </div></td>';       
   echo '<td>' . $data['size'] . '</td>';  
   echo '<td>' . $data['status'] . '</td>';        
   echo '<td>' . $data['date'] . '</td>';  
   echo '<td>' . $data['tags'] . '</td>';                  
   echo '</tr>';       
}       

echo '</tbody>';        
echo '</table>';         
mysql_close($connect_to_db);        
?>

PS Sorry for my english, it's not my native language. PS对不起,我的英语不是我的母语。

if i got it true you want to show data that added for specific date/time or between date/time ranges. 如果我对,您想显示为特定日期/时间或日期/时间范围添加的数据。 you should use mysql query to get data that for example last day added. 您应该使用mysql查询来获取例如最后一天添加的数据。 you should change query: 您应该更改查询:

$qr_result = mysql_query("select * from " . $db_table_to_show)  

first you should save date/time for each row when it added or updated. 首先,应在添加或更新每行时保存日期/时间。 after that you can get today date/time by PHP functions and then use it in your query. 之后,您可以通过PHP函数获取今天的日期/时间,然后在查询中使用它。 an example: 一个例子:

SELECT * FROM [TableName] WHERE `AddedDate` BETWEEN "2012-03-15" AND "2012-03-31";

PHP function to get date : http://php.net/manual/en/function.getdate.php PHP函数获取日期: http : //php.net/manual/en/function.getdate.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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