84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
举个例子,我要获取当前的时间戳,可以这样:
time();
我要获取以当前时间为基础,往后1个月的时间戳,可以这样:
strtotime('+1 month');
我要获取指定时间的时间戳,比如2019-11-23,可以这样:
strtotime('2019-11-23');
那么问题来了,我要以任意一个时间节点作为基础,在此之上获取往后1个月的时间戳,请问怎么做?比如说2019-11-23往后1个月的时间戳。
需要考虑到闰年、闰月、大小月份哦
php How to calculate the timestamp of one month in the next specified time? -PHP Chinese website Q&A-php How to calculate the timestamp of one month in the future at a specified time? -PHP Chinese website Q&A
Let’s take a look and learn.
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')也是类似的结果。
int strtotime ( string $time [, int $now = time() ] )
strtotime 可以传入第二个参数,用来表示参考时间,默认是当前时间,所以 strtotime('+1 month'); 才会计算出下个月的时间。
php How to calculate the timestamp of one month in the next specified time? -PHP Chinese website Q&A-php How to calculate the timestamp of one month in the future at a specified time? -PHP Chinese website Q&A
Let’s take a look and learn.
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天,很可能不是你想要的结果:
DateInterval('P1M')也是类似的结果。
strtotime 可以传入第二个参数,用来表示参考时间,默认是当前时间,所以 strtotime('+1 month'); 才会计算出下个月的时间。