php時間戳記轉換日期方法總結

來源:互聯網
上載者:User

目前時間戳

 代碼如下 複製代碼

$nowcode=time();
$nowcode=strtotime(now);


時間戳記轉化為日期

 代碼如下 複製代碼

$date=date("Y-m-d",getdatecode($a));

日期轉化為時間戳記

 代碼如下 複製代碼
function getdatecode($time)
{
 $year=((int)substr($time,0,4));        //取得年份
 $month=((int)substr($time,5,2));       //取得月份
 $day=((int)substr($time,8,2));         //取得幾號
 return mktime(0,0,0,$month,$day,$year);//反悔UNIX時間戳記
}
 

好,言歸正傳,把1228348800轉成2008-12-4格式代碼如下:

 代碼如下 複製代碼

$date3=date('Y-m-d H:i:s',"1228348800");

這樣就OK了,如還想得到小時,分鐘秒,只要把'Y-m-d'改一下就可以了,不過要注意一下,PHP時間還像有8個小時的誤差.加上就OK了.

可以在程式前面加上

 代碼如下 複製代碼
date_default_timezone_set("Asia/Shanghai");

 
下面有更多的時間戳記轉換日期的例子

 代碼如下 複製代碼

(1)列印明天此時的時間戳記strtotime(”+1 day“)
目前時間:echo date(”Y-m-d H:i:s”,time()) 結果:2009-01-22 09:40:25
指定時間:echo date(”Y-m-d H:i:s”,strtotime(”+1 day”)) 結果:2009-01-23 09:40:25

(2)列印昨天此時的PHP時間戳記strtotime(”-1 day“)
目前時間:echo date(”Y-m-d H:i:s”,time()) 結果:2009-01-22 09:40:25
指定時間:echo date(”Y-m-d H:i:s”,strtotime(”-1 day”)) 結果:2009-01-21 09:40:25

(3)列印下個星期此時的時間戳記strtotime(”+1 week“)
目前時間:echo date(”Y-m-d H:i:s”,time()) 結果:2009-01-22 09:40:25
指定時間:echo date(”Y-m-d H:i:s”,strtotime(”+1 week”)) 結果:2009-01-29 09:40:25

(4)列印上個星期此時的時間戳記strtotime(”-1 week“)
目前時間:echo date(”Y-m-d H:i:s”,time()) 結果:2009-01-22 09:40:25
指定時間:echo date(”Y-m-d H:i:s”,strtotime(”-1 week”)) 結果:2009-01-15 09:40:25

(5)列印指定下星期幾的PHP時間戳記strtotime(”next Thursday“)
目前時間:echo date(”Y-m-d H:i:s”,time()) 結果:2009-01-22 09:40:25
指定時間:echo date(”Y-m-d H:i:s”,strtotime(”next Thursday”)) 結果:2009-01-29 00:00:00

(6)列印指定上星期幾的時間戳記strtotime(”last Thursday“)
目前時間:echo date(”Y-m-d H:i:s”,time()) 結果:2009-01-22 09:40:25
指定時間:echo date(”Y-m-d H:i:s”,strtotime(”last Thursday”)) 結果:2009-01-15 00:00:00

總結,上面所有關於時間戳記轉換日期的一些例子來看上面就是三個函數在這裡轉了一個是strtotime(),date(),time()函數了,如果你想知道它樣的用法可在本站搜尋。

聯繫我們

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