时间:2021-07-01 10:21:17 帮助过:3人阅读
- mysql> select now();
- +---------------------+
- | now() |
- +---------------------+
- | 2008-08-08 22:20:46 |
- +---------------------+
MySQL 获得当前时间戳函数:current_timestamp, current_timestamp()
- mysql> select current_timestamp, current_timestamp();
- +---------------------+---------------------+
- | current_timestamp | current_timestamp() |
- +---------------------+---------------------+
- | 2008-08-09 23:22:24 | 2008-08-09 23:22:24 |
- +---------------------+---------------------+
MySQL Date/Time to Str(日期/时间转换为字符串)函数:date_format(date,format), time_format(time,format)
- mysql> select date_format(‘2008-08-08 22:23:01‘, ‘%Y%m%d%H%i%s‘);
- +----------------------------------------------------+
- | date_format(‘2008-08-08 22:23:01‘, ‘%Y%m%d%H%i%s‘) |
- +----------------------------------------------------+
- | 20080808222301 |
- +----------------------------------------------------+
MySQL (Unix 时间戳、日期)转换函数
- unix_timestamp(),
- unix_timestamp(date),
- from_unixtime(unix_timestamp),
- from_unixtime(unix_timestamp,format)
- select unix_timestamp(); -- 1218290027
- select unix_timestamp(‘2008-08-08‘); -- 1218124800
- select unix_timestamp(‘2008-08-08 12:30:00‘); -- 1218169800
- select from_unixtime(1218290027); -- ‘2008-08-09 21:53:47‘
- select from_unixtime(1218124800); -- ‘2008-08-08 00:00:00‘
- select from_unixtime(1218169800); -- ‘2008-08-08 12:30:00‘
- select from_unixtime(1218169800, ‘%Y %D %M %h:%i:%s %x‘); -- ‘2008 8th August 12:30:00 2008‘
------------------------------------------------------------------------------------------
PHP+Mysql日期时间如何转换
写过PHP+MySQL的程序员都知道有时间差,UNIX时间戳和格式化日期是我们常打交道的两个时间表示形式,Unix时间戳存储、处理方便,但是不直观,格式化日期直观,但是处理起来不如Unix时间戳那么自如,所以有的时候需要互相转换,下面给出互相转换的几种转换方式。
一、在MySQL中完成
这种方式在MySQL查询语句中转换,优点是不占用PHP解析器的解析时间,速度快,缺点是只能用在数据库查询中,有局限性。
1. UNIX时间戳转换为日期用函数: FROM_UNIXTIME()
一般形式:select FROM_UNIXTIME(1156219870);
2. 日期转换为UNIX时间戳用函数: UNIX_TIMESTAMP()
一般形式:Select UNIX_TIMESTAMP(’2006-11-04 12:23:00′);
举例:mysql查询当天的记录数:
$sql=”select * from message Where DATE_FORMAT(FROM_UNIXTIME(chattime),’%Y-%m-%d’) = DATE_FORMAT(NOW(),’%Y-%m-%d’) order by id desc”;
二、在PHP中完成
这种方式在PHP程序中完成转换,优点是无论是不是数据库中查询获得的数据都能转换,转换范围不受限制,缺点是占用PHP解析器的解析时间,速度相对慢。
1. UNIX时间戳转换为日期用函数: date()
一般形式:date(‘Y-m-d H:i:s‘, 1156219870);
2. 日期转换为UNIX时间戳用函数:strtotime()
一般形式:strtotime(‘2010-03-24 08:15:42‘);
MYSQL \ PHP日期函数互相转换
标签: