如何实现日期比较,暨_PHP教程

WBOY
Release: 2016-07-13 10:53:32
Original
893 people have browsed it

前几天发了一张帖子,就是关于显示5天内这种日期比较,
http://www.oso.com.cn/forum/read.php?theme_id=7459
主要的论点是集中在SELECT something FROM table
WHERE TO_DAYS(NOW()) - TO_DAYS(date_col) MYSQL的TO_DAYS(DATE)函数是这样说明的:
传回DATE到公元0年的总日数,我测试了一下
mysql>select to_days(now(0));
+--------------------------+
| TO_DAYS(NOW()) |
+--------------------------+
| 730839 |
+--------------------------+
出来的是当前时间距离公元0年的总日数,接着我试着用上面的语句测试;
mysql>select TO_DAYS(NOW()) - TO_DAYS(date_col) 出现结果:
ERROR 1054:Unknown column 'date_col' in 'field first'
此路不通了,我就试着直接把5代到date_col里去
mysql>select to_days(now()) - to_days(5);
出现结果:
+---------------------------+
|to_days(now()) - to_days(5)|
+---------------------------+
| NULL |
+---------------------------+
啊?不会吧?这样也不行啊?
我接着试命令
mysql>select 。。。。
突然猛的想到,嘿嘿,to_days(now())出来的是整数,我直接跟整数运算就行了,何必再to_days(date)呢?马上试验
mysql>select to_days(now()) - 5;
+--------------------------+
| to_days(now()) -5 |
+--------------------------+
| 730834 |
+--------------------------+
OK,万岁,终于得到了我想要的结果,呵呵 下面就是在PHP代码中用SELECT 查询了
我存数据库向来的习惯就是DATEANDTIME用NOW()直接赋值,显示的时候不用格式化,直接取出来就能用,
下面是我的一个库的部分结构
CREATE TABLE infomess (
infoid int(11) NOT NULL auto_increment,
topic varchar(255) NOT NULL,
……
email varchar(50),
dateandtime datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
PRIMARY KEY (infoid)
);
这里的DATEANDTIME是标准的日期格式,然后我要查询5天内的记录,下面是SQL查询语句

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/632387.htmlTechArticle前几天发了一张帖子,就是关于显示5天内这种日期比较, http://www.oso.com.cn/forum/read.php?theme_id=7459 主要的论点是集中在SELECT something FROM tabl...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!