简体   繁体   English

如何按今天或昨天或特定日期查看新闻?

[英]How can I view news by date Today or Yesterday, or a specific date?

I have table in a sql db 我在SQL数据库中有表

CREATE TABLE `News` (
`Id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`Title` varchar(100),
`Time` int(100)
PRIMARY KEY (`Id`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1;  

and I have a page View.php 我有一个页面View.php

<?php
mysql_select_db($database_config, $config);
$query_m_list = "SELECT * FROM News Where Time='today' ORDER BY Id Asc";
$m_list= mysql_query($query_m_list, $config) or die(mysql_error());
$row_m_list= mysql_fetch_assoc($m_list);
$totalRows_m_list = mysql_num_rows($m_list);
?> 

How can I view news by date Today or yesterday, or a specific date from the table? 如何按日期今天或昨天或表格中的特定日期查看新闻?

Change your Time columns type to datetime and then use the below query: 将您的时间列类型更改为datetime时间,然后使用以下查询:

SELECT * FROM M_News Where date(`Time`)<=CURRENT_DATE() 
AND date(`Time`)>=DATE_SUB(CURRENT_DATE(),INTERVAL 1 DAY)
ORDER BY Id Asc

Above query will give you the news of the today and yesterday. 以上查询将为您提供今天和昨天的新闻。

since you store time as (int) data type you need to use time_ago function to calculate seconds,hours,days,weeks, month and year. 由于您将时间存储为(int)数据类型,因此需要使用time_ago函数来计算秒,小时,天,周,月和年。 you can download jquery timeago here TimeAgo 你可以在这里下载jQuery timeago

Once the timeago functions returns a specific value you can then ponder about fetching correct data you want. 一旦timeago函数返回特定值,您就可以考虑获取所需的正确数据。

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

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