php獲取幾月后日期的方法:1、使用“strtotime("+N month")”語句獲取幾月后的時(shí)間戳,參數(shù)“N”指定具體月數(shù);2、使用“date("Y-m-d",時(shí)間戳)”語句格式化獲取的時(shí)間戳,將其轉(zhuǎn)為“年-月-日”的日期格式。
本教程操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
php怎么獲取幾月后的日期
實(shí)現(xiàn)步驟:
-
使用strtotime() 函數(shù)進(jìn)行日期的加減運(yùn)算,獲取幾月后的時(shí)間戳。
-
使用date() 函數(shù)格式獲取的時(shí)間戳,將其轉(zhuǎn)為指定的時(shí)間日期格式。
實(shí)現(xiàn)代碼:
<?php header("Content-type:text/html;charset=utf-8"); echo "當(dāng)前時(shí)間戳為:".strtotime("now")."<br>"; echo "格式化當(dāng)前時(shí)間戳:".date("Y-m-d H:i:s",strtotime("now"))."<br><br>"; echo "1個(gè)月后的時(shí)間戳:".strtotime("+1 month")."<br>"; echo "格式化時(shí)間戳:".date("Y-m-d H:i:s",strtotime("+1 month"))."<br><br>"; echo "當(dāng)前時(shí)間戳后2個(gè)月:".date("Y-m-d H:i:s",strtotime("+2 month"))."<br>"; echo "當(dāng)前時(shí)間戳后3天:".date("Y-m-d H:i:s",strtotime("+3 month"))."<br>"; echo "當(dāng)前時(shí)間戳后4天:".date("Y-m-d H:i:s",strtotime("+4 month"))."<br>"; ?>
說明:
PHP strtotime() 函數(shù)有兩種用法:一種是將字符串形式的、用英文文本描述的日期時(shí)間解析為 UNIX 時(shí)間戳,一種是用來計(jì)算一些日期時(shí)間的間隔。
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); ?>
推薦學(xué)習(xí):《PHP視頻教程》