PHP入門(8)日期和時間

來源:互聯網
上載者:User

標籤:php入門

PHP語言預設的是格林威治時間,要擷取本地時間必須要修改PHP語言中的時區設定。

修改時區設定有兩種方式

  1. 修改設定檔php.ini

[Date]

date.timezone=Asia/Hong_Kong

2.在應用程式中修改時區

date_default_timezone_set(timezone);

timezone可以設定為"PRC" "Asia/Chongqing" "Asia/Shanghai" 

可以通過下面的代碼驗證

echo date("Y-m-d H:i:s");


Unix時間戳記:自1970年1月1日起到當前時刻經曆的秒數。


擷取當前的時間戳記

int mktime()

echo mktime();echo date("Y-m-d",mktime());//使用date函數格式化後的日期  年-月-日echo date("H:i:s",mktime()); //時:分:秒

還可以通過time()函數擷取當前的時間戳記

int time(void)

//擷取下一周的日期$nextweek = time() + 7*24*60*60;echo "Next week:".date("Y-m-d",$nextweek);


strtotime()可以將英文文本解析為Unix時間戳記

int strtotime(string str);

echo strtotime("now");echo strtotime("+3 day");//三天后的時間戳記echo strtotime("+1 week");//一周后的時間戳記echo strtotime("next Thursday");


以數組的形式返回時間的資訊

array getdate(int timestamp);


這些時間函數不用死記硬背,用的時候查就行。

本文出自 “厚積薄發” 部落格,請務必保留此出處http://joedlut.blog.51cto.com/6570198/1855565

PHP入門(8)日期和時間

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.