php:strtotime(“12/31/2004 + 6个月”));6月的最后一天不回来。

时间:2022-10-16 09:32:21

I expected this functional to return 6/30/2005 instead of 7/1/2005.

我期望这个功能可以返回6/30/2005而不是7/1/2005。

print date("m/d/Y", strtotime("12/31/2004 +6 month"));

Similarly, print date("m/d/Y", strtotime("1/31/2011 +1 month")) returns 03/03/2011 while would like it to return 2/28/2011.

同样,打印日期(“m/d/Y”,strtotime(“1/31/2011 +1个月”))返回03/03/2011,同时希望它返回2/28/2011。

Does anyone know if there is a straight forward way to show the last day of the added month?

有没有人知道是否有一种直接的方式来显示新增月份的最后一天?

4 个解决方案

#1


6  

How about this?

这个怎么样?

echo date("m/d/Y", strtotime("last day of 12/31/2004 + 6 month")); // 6/30/2005
echo date("m/d/Y", strtotime("last day of 1/31/2011 + 1 month")); // 2/28/2011

Demo

演示

Edit: For your reference, here is a link to the documentation for relative times.

编辑:为了您的参考,这里是相关时间文档的链接。

#2


2  

as strtotime continue in to next month if there isn't enoghe days that month, you can back 6 month and check if its end up on the start date

如果下个月还没到月的时候,strtotime还会继续下去,你可以在6个月后回来看看它是否在开始的时候就结束了。

  $date2 = date("Y-m-d", strtotime("{$date} +6 months"));
  $date3 = date("Y-m-d", strtotime("{$date2} -6 months"));
  if($date3 != $date)
  {
     $date2 = date("Y-m-t", strtotime("{$date2} -1 months"));
  }

(or in your case "m/t/Y")

(或以“m/t/Y”为例)

#3


0  

One simple way is to actually go one month ahead of the day you want and then make the day value zero. Also, mktime() might be easier

一种简单的方法是在你想要的那一天提前一个月,然后让一天的价值为零。另外,mktime()可能更简单。

$mymonth = 2;   // I want the last day of February
echo date('m/d/Y', mktime(0,0,0,$mymonth+1,0,2011));

This should return 2/28/2011.

这应该返回2/28/2011。

#4


0  

strtotime does the best it can with conflicting information. Saying

strtotime用冲突的信息做得最好。说

1/31/2011 +1month

would mean advancing to

将意味着推进

2/31/2011

but February only has 28 (sometimes 29) days. 2011 isn't a leap year, so the "31st of February" gets normalized to "March 3rd".

但是2月只有28天(有时是29天)。2011年不是闰年,所以“2月31日”被归为“3月3日”。

The same applies for '12/31/2004 +6month'. That takes you to June 31st, 2005. But June only has 30 days, so the date is normalized to July 1st instead.

同样适用于“12/31/2004 +6月”。到2005年6月31日。但是6月只有30天,所以日期改为7月1日。

#1


6  

How about this?

这个怎么样?

echo date("m/d/Y", strtotime("last day of 12/31/2004 + 6 month")); // 6/30/2005
echo date("m/d/Y", strtotime("last day of 1/31/2011 + 1 month")); // 2/28/2011

Demo

演示

Edit: For your reference, here is a link to the documentation for relative times.

编辑:为了您的参考,这里是相关时间文档的链接。

#2


2  

as strtotime continue in to next month if there isn't enoghe days that month, you can back 6 month and check if its end up on the start date

如果下个月还没到月的时候,strtotime还会继续下去,你可以在6个月后回来看看它是否在开始的时候就结束了。

  $date2 = date("Y-m-d", strtotime("{$date} +6 months"));
  $date3 = date("Y-m-d", strtotime("{$date2} -6 months"));
  if($date3 != $date)
  {
     $date2 = date("Y-m-t", strtotime("{$date2} -1 months"));
  }

(or in your case "m/t/Y")

(或以“m/t/Y”为例)

#3


0  

One simple way is to actually go one month ahead of the day you want and then make the day value zero. Also, mktime() might be easier

一种简单的方法是在你想要的那一天提前一个月,然后让一天的价值为零。另外,mktime()可能更简单。

$mymonth = 2;   // I want the last day of February
echo date('m/d/Y', mktime(0,0,0,$mymonth+1,0,2011));

This should return 2/28/2011.

这应该返回2/28/2011。

#4


0  

strtotime does the best it can with conflicting information. Saying

strtotime用冲突的信息做得最好。说

1/31/2011 +1month

would mean advancing to

将意味着推进

2/31/2011

but February only has 28 (sometimes 29) days. 2011 isn't a leap year, so the "31st of February" gets normalized to "March 3rd".

但是2月只有28天(有时是29天)。2011年不是闰年,所以“2月31日”被归为“3月3日”。

The same applies for '12/31/2004 +6month'. That takes you to June 31st, 2005. But June only has 30 days, so the date is normalized to July 1st instead.

同样适用于“12/31/2004 +6月”。到2005年6月31日。但是6月只有30天,所以日期改为7月1日。