我期望这个函数返回6/30/2005而不是7/1/2005。
print date("m/d/Y", strtotime("12/31/2004 +6 month"));类似地,print date("m/d/Y", strtotime("1/31/2011 +1 month"))返回03/03/2011,而希望它返回2/28/2011。
有没有人知道有没有一种直接的方法来显示添加月份的最后一天?
发布于 2011-11-12 04:50:11
这个怎么样?
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/2011Demo
编辑:供您参考,这里有一个指向documentation for relative times的链接。
发布于 2011-11-12 05:12:17
当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"));
}(或在您的情况下为"m/t/Y")
发布于 2011-11-12 05:18:08
strtotime在处理冲突信息方面做得最好。说
1/31/2011 +1month将意味着前进到
2/31/2011但二月份只有28天(有时是29天)。2011年不是闰年,所以“2月31日”被标准化为“3月3日”。
这同样适用于'12/31/2004 +6个月‘。把你带到2005年6月31日。但是6月只有30天,所以日期被标准化为7月1日。
https://stackoverflow.com/questions/8099719
复制相似问题