php如何获取几月后的日期
本文小编为大家详细介绍“php如何获取几月后的日期”,内容详细,步骤清晰,细节处理妥当,希望这篇“php如何获取几月后的日期”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。
php获取几月后日期的方法:1、使用“strtotime("+N month")”语句获取几月后的时间戳,参数“N”指定具体月数;2、使用“date("Y-m-d",时间戳)”语句格式化获取的时间戳,将其转为“年-月-日”的日期格式。
本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑
php怎么获取几月后的日期
实现步骤:
使用strtotime() 函数进行日期的加减运算,获取几月后的时间戳。
使用date() 函数格式获取的时间戳,将其转为指定的时间日期格式。
实现代码:
<?phpheader("Content-type:text/html;charset=utf-8");echo"当前时间戳为:".strtotime("now")."<br>";echo"格式化当前时间戳:".date("Y-m-dH:i:s",strtotime("now"))."<br><br>";echo"1个月后的时间戳:".strtotime("+1month")."<br>";echo"格式化时间戳:".date("Y-m-dH:i:s",strtotime("+1month"))."<br><br>";echo"当前时间戳后2个月:".date("Y-m-dH:i:s",strtotime("+2month"))."<br>";echo"当前时间戳后3天:".date("Y-m-dH:i:s",strtotime("+3month"))."<br>";echo"当前时间戳后4天:".date("Y-m-dH:i:s",strtotime("+4month"))."<br>";?>
说明:
PHP strtotime() 函数有两种用法:一种是将字符串形式的、用英文文本描述的日期时间解析为 UNIX 时间戳,一种是用来计算一些日期时间的间隔。
strtotime() 函数的使用示例如下:
<?phpechostrtotime("now"),"<br/>";echostrtotime("10September2000"),"<br/>";echostrtotime("+1day"),"<br/>";echostrtotime("+1week"),"<br/>";echostrtotime("+1week2days4hours2seconds"),"<br/>";echostrtotime("nextThursday"),"<br/>";echostrtotime("lastMonday"),"<br/>";?>
<?phpheader("Content-type:text/html;charset=utf-8");$start='lastMonday';$interval=strtotime("$start+4days");echo"现在\$interval表示上周的".date('l',$interval);?>
读到这里,这篇“php如何获取几月后的日期”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。