1月份使用+ 1个月时,“strtotime”错误 [英] `strtotime` bug when using + 1 month from January

查看:143
本文介绍了1月份使用+ 1个月时,“strtotime”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为先前输入的日期范围制作多个标签的ajax日历。
但是例如:



我想要下个月,它打印游行而不是二月

  $ start =2013-01-31; 
$ current = date('n',strtotime(+ 1 month,$ start))//打印3

我认为这是因为2014年2月28日起,从起始月份起添加+31喜欢基数,但为什么?

解决方案

您正尝试添加一个月的日期 2013-01-31 。它应该提供2013年的第二十一号,但是由于日期不存在,它将转到下一个有效的月份(即3月)。



您可以使用以下内容解决方法:

  $ current = date('n',strtotime(下个月的第一天,strtotime开始))); 

使用 DateTime class:

  $ date = new DateTime('2013-01-31'); 
$ date-> modify('下个月的第一天');
echo $ date-> format('n');

这将正确输出 2 p>

演示!


I' m trying to make a ajax calendar with multiple tabs for a date range previously entered. But for example:

I want get the next month, it prints march instead of february

$start= "2013-01-31";
$current =  date('n', strtotime("+1 month",$start)) //prints 3

I think thats occurs because february 2014 is 28 and add +31 like base from the start month but why?

解决方案

You're trying to add one month to the date 2013-01-31. It should give 31th Feburary 2013, but since the date doesn't exist, it moves on to the next valid month (which is March).

You can use the following work-around:

$current = date('n', strtotime("first day of next month",strtotime($start)));

Using DateTime class:

$date = new DateTime('2013-01-31');
$date->modify('first day of next month');
echo $date->format('n');

This will correctly output 2.

Demo!

这篇关于1月份使用+ 1个月时,“strtotime”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆