標籤:
MySQL 獲得當前日期時間 函數
來源:http://www.cnblogs.com/ggjucheng/p/3352280.html
獲得當前日期+時間(date + time)函數:now()
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 日期轉換函式、時間轉換函式
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(); -- 1218290027select unix_timestamp(‘2008-08-08‘); -- 1218124800select unix_timestamp(‘2008-08-08 12:30:00‘); -- 1218169800select 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日期函數互相轉換