php指定时间戳加上月,php 如何计算指定时间,往后1个月的时间戳

举个例子,我要获取当前的时间戳,可以这样:

time();

我要获取以当前时间为基础,往后1个月的时间戳,可以这样:

strtotime('+1 month');

我要获取指定时间的时间戳,比如

2019-11-23

,可以这样:

strtotime('2019-11-23');

那么问题来了,我要以任意一个时间节点作为基础,在此之上获取往后1个月的时间戳,请问怎么做?比如说

2019-11-23

往后1个月的时间戳。

需要考虑到闰年、闰月、大小月份哦

int strtotime ( string $time [, int $now = time() ] )

strtotime

可以传入第二个参数,用来表示参考时间,默认是当前时间,所以

strtotime('+1 month');

才会计算出下个月的时间。

这个么?

<?php

$date = new DateTime('2019-11-23');

$date->add(new DateInterval('P1M'));

echo $date->format('Y-m-d') . "\n";

?>

1月31日往后一个月是二月28/29号,还是3月1/2/3号?

2月28/29日往后一个月是3月28/29/30/31?

3月31日往后一个月是4月30号还是5月1号?

strtotime

+1 month

是直接加31天,很可能不是你想要的结果:

date('Y-m-d', strtotime('+1 month', strtotime('2016-01-31'))); // 2016-03-02

date('Y-m-d', strtotime('+1 month', strtotime('1999-01-31'))); // 1999-03-03

date('Y-m-d', strtotime('+1 month', strtotime('1999-03-31'))); // 1999-05-01

DateInterval('P1M')

也是类似的结果。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值