PHP中strtotime函數用法_PHP教程

來源:互聯網
上載者:User
strtotime函數在php中是可以把日誌轉換成時間戳記,他可以方便的讓我們對日期時間分秒的計算,下面我來給各位介紹時strtotime函數用法,有需要的同學可參考。

1.擷取當前的時間的時間戳記!

a.實用strtotime('now');來擷取當前的時間戳記!(因為PHP目前時間和真是時間相差8小時)

代碼如下 複製代碼

1 echo date('Y-m-d H:i:s',strtotime('now')+8*60*60);
結果:2013-04-05 03:15:02

b.你也可以用time() 來直接擷取!

代碼如下 複製代碼


1 echo date('Y-m-d H:i:s',time()+8*60*60);
結果:2013-04-05 03:15:02

2.擷取10天之後的時間戳記:

使用方法:

代碼如下 複製代碼
1 strtotime("+10 days")+8*60*60

輸出10天后的日期,便於查看。

代碼如下 複製代碼

1 echo date('Y-m-d H:i:s',strtotime("+10 days")+8*60*60);

結果:2013-04-15 03:15:02

3.擷取一周的時間戳記:


使用方法:

代碼如下 複製代碼

1 strtotime("+1 week")+8*60*60

或者你使用上面按天來擷取時間戳記.寫法如:

代碼如下 複製代碼

strtotime("+7 days");

4.還可以擷取指定幾周幾天幾小時幾秒來擷取時間戳記

使用方法:

代碼如下 複製代碼

1 strtotime ("+1 week 2 days 4 hours 2 seconds")+8*60*60
1 echo date('Y-m-d H:i:s',strtotime ("+1 week 2 days 4 hours 2 seconds")+8*60*60);

結果:2013-04-12 07:15:04;

5.擷取上周一或者下周四的時間戳記:

使用方法:

a.擷取上周一的時間戳記:

代碼如下 複製代碼

1 strtotime ("last Monday")+8*60*60
1 echo date('Y-m-d H:i:s',strtotime ("last Monday")+8*60*60);

結果:2013-04-01 08:00:00;


b.擷取下周四的時間戳記

代碼如下 複製代碼

1 strtotime ("next Thursday")+8*60*60
1 echo date('Y-m-d H:i:s',strtotime ("next Thursday")+8*60*60);

結果:2013-04-11 08:00:00;


strtotime函數比較兩個時間的大小


strtotime()函數對兩個固定的時間進行比較,如下:
1):定義兩個固定的時間;

2):通過strtotime()函數將固定時間轉換成時間戳記;

3):對兩個時間戳記的值進行比較。

其執行個體代碼如下:

代碼如下 複製代碼

$time="2012年11月23日15時50時20秒";
$times="2013-01-14 09:09:09";
if(strtotime($time)-strtotime($times)<0){
echo "時間:".$time." 早於時間:".$times;
}else{
echo "時間:".$times." 早於時間:".$time;
}
echo "
它們相差的時間值是:".(strtotime($time)-strtotime($times))
?>

上面的代碼運行結果如下:

時間:2012年11月23日15時50時20秒 早於時間:2013-01-14 09:09:09
它們相差的時間值是:-1358125749


http://www.bkjia.com/PHPjc/445625.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445625.htmlTechArticlestrtotime函數在php中是可以把日誌轉換成時間戳記,他可以方便的讓我們對日期時間分秒的計算,下面我來給各位介紹時strtotime函數用法,有需...

  • 聯繫我們

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