天天看點

PHP的時間函數strtotime

時間加減

[php] view plain copy

  1. <?php
  2. //擷取本地提取年份+1
  3. $date=date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y")+1));
  4. ?>

如果要擷取資料庫中的時間應該如何處理呢?在PHP文檔中找到了一個很好的函數strtotime,可以對時間進行加減:

[php] view plain copy

  1. intstrtotime(stringtime[,intnow]) 

傳回類型 int

函數可用于時間轉換和加減。

[php] view plain copy

  1. <?php
  2. //date()格式化時間傳回String類型。
  3. //加一年
  4. $date_year=date('Y-m-d',strtotime("$date+1year"));
  5. echo$date_year;
  6. //加一天
  7. $date_tomorrow=date('Y-m-d',strtotime("2009-05-26+1day"));
  8. echo$date_tomorrow;
  9. //加一周
  10. $date_week=date('Y-m-d',strtotime("$date+1week"));
  11. echo$date_week;
  12. ?>
$time = strtotime('2014-06-10 00:06:00');
var_dump($time); //int 1402358760