php&mysql 日期操作小記
更新時間:2012年02月27日 21:34:21 作者:
在php的很多成熟框架中,數(shù)據(jù)庫存儲時間都是用int類型而不是datetime類型的
在時間比較查詢的時候,int的效率明顯更高。祥文見http://www.dbjr.com.cn/article/29767.htm
但是在做項目的時候或者直接在數(shù)據(jù)庫查看數(shù)據(jù)的時候,明顯這個int一看頭就大,比如我們想
要查看一個用戶的注冊時間:
select reg_time from t_xx_users where user_id=1;
這時候返回是個int值,不能直觀的看到具體的時間,所以這時候就涉及到datetime和int的轉(zhuǎn)化問題,
還有php的date和time也是要涉及到相應的轉(zhuǎn)化。本文略總結(jié)一下:
(1)php
int值:
time():是返回自從 Unix 紀元(格林威治時間 1970 年 1 月 1 日 00:00:00)到當前時間的秒數(shù)。
我們想要獲得1970 年 1 月 1 日到 2012-2-10的秒數(shù)可以通過strtotime()來實現(xiàn):即:strtotime('2012-2-10');
date值:
string date ( string format [, int timestamp] )
比如:直接date()返回的的實現(xiàn)當前的時間,當然我們可以指定的他的格式:例如date('Y-m-d',strtotime('2012-2-10'));
時間操作:
date('Y-m-d h:i:s',strtotime('+1 week'));
date('Y-m-d h:i:s',strtotime('+5 hours'));
date('Y-m-d h:i:s',strtotime('next Monday));
date('Y-m-d h:i:s',strtotime('last Sunday'));
date('Y-m-d h:i:s',strtotime('+ 1 day',12313223));!!詳見 int strtotime ( string time [, int now] )
(2)mysql:
int->datetime
select from_unixtime(int_time) from table;
datetime->int;
select unix_timestamp(date_time) from table;
時間操作:
select dayofweek('2012-2-2');返回一個星期的第幾天
select dayofmonth('2012-2-2');返回一月中的第幾天
select dayofyear('2012-2-2');返回一年中的第幾天
類似函數(shù): month() day() hour() week()......
+幾天 date_add(date,interval 2 days);
-幾天 date_sub(date,interval 2 days);
時間格式:
date_format(date,format)
select DATE_FORMAT('1997-10-04 22:23:00','%W %M %Y');
其他函數(shù):TIME_TO_SEC() SEC_TO_TIME()...
但是在做項目的時候或者直接在數(shù)據(jù)庫查看數(shù)據(jù)的時候,明顯這個int一看頭就大,比如我們想
要查看一個用戶的注冊時間:
select reg_time from t_xx_users where user_id=1;
這時候返回是個int值,不能直觀的看到具體的時間,所以這時候就涉及到datetime和int的轉(zhuǎn)化問題,
還有php的date和time也是要涉及到相應的轉(zhuǎn)化。本文略總結(jié)一下:
(1)php
int值:
time():是返回自從 Unix 紀元(格林威治時間 1970 年 1 月 1 日 00:00:00)到當前時間的秒數(shù)。
我們想要獲得1970 年 1 月 1 日到 2012-2-10的秒數(shù)可以通過strtotime()來實現(xiàn):即:strtotime('2012-2-10');
date值:
string date ( string format [, int timestamp] )
比如:直接date()返回的的實現(xiàn)當前的時間,當然我們可以指定的他的格式:例如date('Y-m-d',strtotime('2012-2-10'));
時間操作:
date('Y-m-d h:i:s',strtotime('+1 week'));
date('Y-m-d h:i:s',strtotime('+5 hours'));
date('Y-m-d h:i:s',strtotime('next Monday));
date('Y-m-d h:i:s',strtotime('last Sunday'));
date('Y-m-d h:i:s',strtotime('+ 1 day',12313223));!!詳見 int strtotime ( string time [, int now] )
(2)mysql:
int->datetime
select from_unixtime(int_time) from table;
datetime->int;
select unix_timestamp(date_time) from table;
時間操作:
select dayofweek('2012-2-2');返回一個星期的第幾天
select dayofmonth('2012-2-2');返回一月中的第幾天
select dayofyear('2012-2-2');返回一年中的第幾天
類似函數(shù): month() day() hour() week()......
+幾天 date_add(date,interval 2 days);
-幾天 date_sub(date,interval 2 days);
時間格式:
date_format(date,format)
select DATE_FORMAT('1997-10-04 22:23:00','%W %M %Y');
其他函數(shù):TIME_TO_SEC() SEC_TO_TIME()...
您可能感興趣的文章:
- MySQL中日期比較時遇到的編碼問題解決辦法
- PHP以及MYSQL日期比較方法
- mysql 獲取當前日期函數(shù)及時間格式化參數(shù)詳解
- MySql用DATE_FORMAT截取DateTime字段的日期值
- mysql unix準換時間格式查找指定日期數(shù)據(jù)代碼
- MySql日期查詢語句詳解
- 深入mysql YEAR() MONTH() DAYOFMONTH()日期函數(shù)的詳解
- mysql 查詢指定日期時間內(nèi)sql語句實現(xiàn)原理與代碼
- PHP+Mysql日期時間如何轉(zhuǎn)換(UNIX時間戳和格式化日期)
- MySQL 日期時間函數(shù)常用總結(jié)
- MySQL 獲得當前日期時間的函數(shù)小結(jié)
- mysql中取系統(tǒng)當前時間,當前日期方便查詢判定的代碼
- Mysql 日期時間 DATE_FORMAT(date,format)
- Mysql中日期和時間函數(shù)應用不用求人
- mysql的日期和時間函數(shù)
- MySQL日期數(shù)據(jù)類型、時間類型使用總結(jié)
- MySQL的Data_ADD函數(shù)與日期格式化函數(shù)說明
- mysql中常用日期比較與計算函數(shù)
相關文章
淺析php fwrite寫入txt文件的時候用 \r\n不能換行的問題
以下是對php中fwrite寫入txt文件的時候用 \r\n不能換行的問題進行了介紹,需要的朋友可以過來參考下2013-08-08php array_merge下進行數(shù)組合并的代碼
array_merge();合并兩個數(shù)組,如果數(shù)組的鍵名是字符,而且兩個數(shù)組的鍵名是相同的字符,2008-07-07