php獲取幾月后日期的方法:1、使用“strtotime("+N month")”語句獲取幾月后的時間戳,參數(shù)“N”指定具體月數(shù);2、使用“date("Y-m-d",時間戳)”語句格式化獲取的時間戳,將其轉(zhuǎn)為“年-月-日”的日期格式。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
php怎么獲取幾月后的日期
實現(xiàn)步驟:
-
使用strtotime() 函數(shù)進行日期的加減運算,獲取幾月后的時間戳。
-
使用date() 函數(shù)格式獲取的時間戳,將其轉(zhuǎn)為指定的時間日期格式。
實現(xiàn)代碼:
<?php header("Content-type:text/html;charset=utf-8"); echo "當前時間戳為:".strtotime("now")."<br>"; echo "格式化當前時間戳:".date("Y-m-d H:i:s",strtotime("now"))."<br><br>"; echo "1個月后的時間戳:".strtotime("+1 month")."<br>"; echo "格式化時間戳:".date("Y-m-d H:i:s",strtotime("+1 month"))."<br><br>"; echo "當前時間戳后2個月:".date("Y-m-d H:i:s",strtotime("+2 month"))."<br>"; echo "當前時間戳后3天:".date("Y-m-d H:i:s",strtotime("+3 month"))."<br>"; echo "當前時間戳后4天:".date("Y-m-d H:i:s",strtotime("+4 month"))."<br>"; ?>
說明:
PHP strtotime() 函數(shù)有兩種用法:一種是將字符串形式的、用英文文本描述的日期時間解析為 UNIX 時間戳,一種是用來計算一些日期時間的間隔。
strtotime() 函數(shù)的使用示例如下:
<?php echo strtotime("now"), "<br />"; echo strtotime("10 September 2000"), "<br />"; echo strtotime("+1 day"), "<br />"; echo strtotime("+1 week"), "<br />"; echo strtotime("+1 week 2 days 4 hours 2 seconds"), "<br />"; echo strtotime("next Thursday"), "<br />"; echo strtotime("last Monday"), "<br />"; ?>
<?php header("Content-type:text/html;charset=utf-8"); $start = 'last Monday'; $interval = strtotime("$start + 4 days"); echo "現(xiàn)在$interval 表示上周的" . date('l',$interval); ?>
推薦學習:《PHP視頻教程》