php mysql日期操作函數

來源:互聯網
上載者:User

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()函數可參考 http://www.111cn.net/phper/php-function/38146.htm

比如:直接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] )

date的參數

每一個參數的格式分別表示:
a - "am" 或是 "pm"
a - "am" 或是 "pm"
d - 幾日,二位元字,若不足二位則前面補零; 如: "01" 至 "31"
d - 星期幾,三個英文字母; 如: "fri"
f - 月份,英文全名; 如: "january"
h - 12 小時制的小時; 如: "01" 至 "12"
h - 24 小時制的小時; 如: "00" 至 "23"
g - 12 小時制的小時,不足二位不補零; 如: "1" 至 12"
g - 24 小時制的小時,不足二位不補零; 如: "0" 至 "23"
i - 分鐘; 如: "00" 至 "59"
j - 幾日,二位元字,若不足二位不補零; 如: "1" 至 "31"
l - 星期幾,英文全名; 如: "friday"
m - 月份,二位元字,若不足二位則在前面補零; 如: "01" 至 "12"
n - 月份,二位元字,若不足二位則不補零; 如: "1" 至 "12"
m - 月份,三個英文字母; 如: "jan"
s - 秒; 如: "00" 至 "59"
s - 字尾加英文序數,二個英文字母; 如: "th","nd"
t - 指定月份的天數; 如: "28" 至 "31"
u - 總秒數
w - 數字型的星期幾,如: "0" (星期日) 至 "6" (星期六)
y - 年,四位元字; 如: "1999"
y - 年,二位元字; 如: "99"
z - 一年中的第幾天; 如: "0" 至 "365"
其它不在上列的字元則直接列出該字元


(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()...

最後我們總結一下關於在使用時間時在資料庫是是用int ,varchar ,datetime這幾種的查詢效率比較吧。

測試四個表的更新,分別update 100條記錄,並記錄時間:北京PHP資源分享門戶d*V.~x G/QO

表一:頁面已耗用時間: 2.62180089951 秒(非定長,int時間)北京PHP資源分享門戶

表二:頁面已耗用時間: 2.5475358963 秒(定長,int時間)
表三:頁面已耗用時間: 2.45077300072 秒(varchar,datetime時間)

表四:頁面已耗用時間: 2.82798409462 秒(char,datetime時間)


大資料量下,如果存在大量的select * from table where 時間>XX這樣的查詢,在MySQL5.1時使用int換datetime是有意義的。


php mysql日期比較代碼
更多更好詳細http://www.111cn.net/phper/21/d40b994ac43e630f316940ea4976564c.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.