本教程操作环境:windows7系统、php7.1版、dell g3电脑
php怎么获取几月后的日期
实现步骤:
使用strtotime() 函数进行日期的加减运算,获取几月后的时间戳。
使用date() 函数格式获取的时间戳,将其转为指定的时间日期格式。
实现代码:
<?phpheader("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() 函数有两种用法:一种是将字符串形式的、用英文文本描述的日期时间解析为 unix 时间戳,一种是用来计算一些日期时间的间隔。
strtotime() 函数的使用示例如下:
<?phpecho 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 />";?>
<?phpheader("content-type:text/html;charset=utf-8");$start = 'last monday';$interval = strtotime("$start + 4 days");echo "现在\$interval 表示上周的" . date('l',$interval);?>
推荐学习:《php视频教程》
以上就是php怎么获取几月后的日期的详细内容。
