创建表
DROP TABLE IF EXISTS `onethink_member`;
CREATE TABLE `onethink_member`(
`uid`int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户ID',
`nickname`char(16) NOT NULL DEFAULT '' COMMENT '昵称',
`sex`tinyint(3) unsigned NOT NULL DEFAULT '0' COMMENT '性别',
`birthday`date NOT NULL DEFAULT '0000-00-00' COMMENT '生日',
`qq`char(10) NOT NULL DEFAULT '' COMMENT 'qq号',
`score`mediumint(8) NOT NULL DEFAULT '0' COMMENT '用户积分',
`login`int(10) unsigned NOT NULL DEFAULT '0' COMMENT '登录次数',
`reg_ip`bigint(20) NOT NULL DEFAULT '0' COMMENT '注册IP',
`reg_time`int(10) unsigned NOT NULL DEFAULT '0' COMMENT '注册时间',
`last_login_ip`bigint(20) NOT NULL DEFAULT '0' COMMENT '最后登录IP',
`last_login_time`int(10) unsigned NOT NULL DEFAULT '0' COMMENT '最后登录时间',
`status`tinyint(4) NOT NULL DEFAULT '0' COMMENT '会员状态',
PRIMARY KEY (`uid`),
KEY `status`(`status`)
) ENGINE=MyISAM AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COMMENT='会员表';
INSERT INTO `onethink_member`VALUES ('1', 'admin', '0', '0000-00-00', '', '10', '2', '0', '1467965167', '0', '1468205456', '1');
INSERT INTO `onethink_member`VALUES ('2', 'test001', '0', '0000-00-00', '', '10', '2', '0', '1487905248', '0', '1487905248', '1');
INSERT INTO `onethink_member`VALUES ('3', 'test002', '0', '0000-00-00', '', '10', '2', '0', '1467965167', '0', '1468205456', '1');
INSERT INTO `onethink_member`VALUES ('4', 'test003', '0', '0000-00-00', '', '10', '2', '0', '1487905249', '0', '1487905248', '1');
INSERT INTO `onethink_member`VALUES ('5', 'test004', '0', '0000-00-00', '', '10', '2', '0', '1487905228', '0', '1487905248', '1');
INSERT INTO `onethink_member`VALUES ('6', 'test005', '0', '0000-00-00', '', '10', '2', '0', '1487905268', '0', '1487905248', '1');
SELECT COUNT(*) FROM onethink_member where DATE(FROM_UNIXTIME(reg_time))= CURDATE();
方法二:
SELECT COUNT(*) FROM onethink_member where DATE_FORMAT(FROM_UNIXTIME(reg_time),'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d');或:SELECT COUNT(*) FROM onethink_member where FROM_UNIXTIME(reg_time,'%Y-%m-%d') = DATE_FORMAT(NOW(),'%Y-%m-%d');
方法三:
SELECT COUNT(*) FROM onethink_member where TO_DAYS(FROM_UNIXTIME(reg_time))= TO_DAYS(NOW());
常用日期函数总结:
SELECT NOW();
2.
DATE()
函数返回日期或日期/时间表达式的日期部分。
![](https://img-blog.csdn.net/20170628172336652?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva3VucGVuZzE5ODc=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
SELECT FROM_DAYS(729669);
5.STR_TO_DATE()
把字符串转换为日期。
7.DATE_ADD()
函数向日期添加指定的时间间隔。
NOW() | CURDATE() | CURTIME() |
---|---|---|
2008-12-29 16:25:46 | 2008-12-29 | 16:25:46 |
SELECT DATE(now());
3.
TO_DAYS(date)
给出一个日期
date
,返回一个天数(从 0 年开始的天数)。
SELECT TO_DAYS(now());
如:
SELECT TO_DAYS(950501);
//
728779
4 .FROM_DAYS(N)
给出一个天数
N
,返回一个
DATE
值。
SELECT STR_TO_DATE(now(),'%Y-%m-%d %H:%i:%s');
6. DATE_FORMAT()函数用于以不同的格式显示日期/时间数据。
SELECT DATE_FORMAT(NOW(),'%Y-%m-%d %H:%i:%s');
SELECT DATE_ADD(NOW(),INTERVAL 30 DAY);
SELECT DATE_SUB(NOW(),INTERVAL 30 MONTH);
SELECT EXTRACT(YEAR FROM NOW()); 等价于:
SELECT YEAR(NOW());
SELECT DATEDIFF('2017-1-30','2015-2-19') AS DiffDate;
11.UNIX_TIMESTAMP()
函数把日期转换为时间戳。
SELECT UNIX_TIMESTAMP(NOW());
12.FROM_UNIXTIME()
函数把时间戳转为日期,还可以加上格式化。
①
SELECT FROM_UNIXTIME(1487919350);
②
SELECT FROM_UNIXTIME(1487919350,'%Y%m%d');