Home >php教程 >php手册 >PHP以及MYSQL日期比较方法

PHP以及MYSQL日期比较方法

WBOY
WBOYOriginal
2016-06-13 11:56:301382browse

MYSQL日期比较
TO_DAYS(NOW()) - TO_DAYS(date_col) 我们的date_col必须是日期时间格式,而不能是时间戳,

mysql> SELECT FROM_UNIXTIME(875996580);
-> '1997-10-04 22:23:00'

day(FROM_UNIXTIME(1277709862))这个返回的是某个时间戳到现在经过的天数

获取 日:day(FROM_UNIXTIME(add_time))
获取 月:month(FROM_UNIXTIME(add_time))
获取 年:year(FROM_UNIXTIME(add_time))
sql文里面获得时间戳后格式化为日,月,年:day(now()),month(now()),year(now())
其中add_time是字段名,存放格式为linux时间戳.

mysql> set @a = unix_timestamp();
Query OK, 0 rows affected (0.00 sec)
mysql> select @a;
+------------+
| @a |
+------------+
| 1154942084 |
+------------+
1 row in set (0.00 sec)
mysql> select from_unixtime(@a);
+---------------------+
| from_unixtime(@a) |
+---------------------+
| 2006-08-07 17:14:44 |
+---------------------+
1 row in set (0.00 sec)

Statement:
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