希望创建一个可以在 PHP 中执行此操作的函数。我需要将月份数添加到日期,但不能超过该月的最后一天。例如:将 1 个月添加到 1 月(...
希望创建一个可以在 PHP 中执行此操作的函数。
我需要向某个日期添加月份数,但不能超过该月的最后一天。
例如:
Add 1 month to January (1-28th), 2011, should produce February (1-28th), 2011.
Add 1 month to January 30th, 2011, should produce February 28th, 2011.
Add 3 months to January 31st, 2011, should produce April 30th, 2011.
Add 13 months to January 30th, 2011, should produce February 29th, 2012.
Add 1 month to October 31st, 2011, should produce November 30th, 2011.
如果我在 PHP 中使用日期加法,则会出现溢出:
Adding 1 month to January 30th, 2011, results in March 2nd, 2011.
我的规范不允许我超出新的月份。
实现这一目标的最简单的方法是什么?