我是靠谱客的博主 忧郁橘子,这篇文章主要介绍php怎么获取几月后的日期,现在分享给大家,希望可以做个参考。

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑

php怎么获取几月后的日期

实现步骤:

  • 使用strtotime() 函数进行日期的加减运算,获取几月后的时间戳。

  • 使用date() 函数格式获取的时间戳,将其转为指定的时间日期格式。

实现代码:

复制代码
1
2
3
4
5
6
7
8
9
10
<?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>"; ?>
登录后复制

1.png

说明:

PHP strtotime() 函数有两种用法:一种是将字符串形式的、用英文文本描述的日期时间解析为 UNIX 时间戳,一种是用来计算一些日期时间的间隔。

strtotime() 函数的使用示例如下:

复制代码
1
2
3
4
5
6
7
8
9
<?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 />"; ?>
登录后复制

2.png

复制代码
1
2
3
4
5
6
<?php header("Content-type:text/html;charset=utf-8"); $start = 'last Monday'; $interval = strtotime("$start + 4 days"); echo "现在$interval 表示上周的" . date('l',$interval); ?>
登录后复制

3.png

推荐学习:《PHP视频教程》

以上就是php怎么获取几月后的日期的详细内容,更多请关注靠谱客其它相关文章!

最后

以上就是忧郁橘子最近收集整理的关于php怎么获取几月后的日期的全部内容,更多相关php怎么获取几月后内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(92)

评论列表共有 0 条评论

立即
投稿
返回
顶部