Home > Database > Mysql Tutorial > mysql查询时间戳和日期的转换_MySQL

mysql查询时间戳和日期的转换_MySQL

WBOY
Release: 2016-06-01 13:28:39
Original
1262 people have browsed it

bitsCN.com

mysql查询时间戳和日期的转换

 

在数据库的使用中,经常需要按指定日期来查询记录,以便于统计,而在数据库中,有很多存储的是时间戳,

 

也有的直接存日期,查询的时候可能不是那么好弄.

 

mysql提供了两个函数:

 

          from_unixtime(time_stamp)   ->  将时间戳转换为日期

 

          unix_timestamp(date)             ->  将指定的日期或者日期字符串转换为时间戳

 

 

如:   from_unixtime(time_stamp)

 

[plain] select from_unixtime(1382544000);  +---------------------------+  | from_unixtime(1382544000) |  +---------------------------+  | 2013-10-24 00:00:00       |  +---------------------------+  如: unix_timestamp(date) [plain] select unix_timestamp(date(&#39;2013-10-24&#39;));  +------------------------------------+  | unix_timestamp(date(&#39;2013-10-24&#39;)) |  +------------------------------------+  |                         1382544000 |  +------------------------------------+  如果要查询当天的订单的记录:[plain] select count(*) from b_order Where  date_format(from_unixtime(create_time),&#39;%Y-%m-%d&#39;) = date_format(now(),&#39;%Y-%m-%d&#39;)  也可以这样:[plain] select count(*) from b_order Where  create_time >= unix_timestamp(&#39;2013-10-24 00:00:00&#39;) and create_time <=  unix_timestamp(&#39;2013-10-24 23:59:59&#39;) ;  
Copy after login

 


bitsCN.com
Related labels:
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