php&mysql 日期操作小記_PHP教程

來源:互聯網
上載者:User
在時間比較查詢的時候,int的效率明顯更高。祥文見http://www.jb51.net/article/29767.htm
但是在做項目的時候或者直接在資料庫查看資料的時候,明顯這個int一看頭就大,比如我們想
要查看一個使用者的註冊時間:
select reg_time from t_xx_users where user_id=1;
這時候返回是個int值,不能直觀的看到具體的時間,所以這時候就涉及到datetime和int的轉化問題,
還有php的date和time也是要涉及到相應的轉化。本文略總結一下:
(1)php
int值:
time():是返回自從 Unix 紀元(格林威治時間 1970 年 1 月 1 日 00:00:00)到目前時間的秒數。
我們想要獲得1970 年 1 月 1 日到 2012-2-10的秒數可以通過strtotime()來實現:即:strtotime('2012-2-10');
date值:
string date ( string format [, int timestamp] )
比如:直接date()返回的的實現當前的時間,當然我們可以指定的他的格式:例如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');返回一年中的第幾天
類似函數: 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');
其他函數:TIME_TO_SEC() SEC_TO_TIME()...

http://www.bkjia.com/PHPjc/325101.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/325101.htmlTechArticle在時間比較查詢的時候,int的效率明顯更高。祥文見http://www.jb51.net/article/29767.htm 但是在做項目的時候或者直接在資料庫查看資料的時候,明...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.