diff --git a/composer.json b/composer.json index d9ac62c..6e002a2 100755 --- a/composer.json +++ b/composer.json @@ -10,7 +10,7 @@ ], "homepage": "https://github.com/phpzm/helper", "license": "MIT", - "version": "1.0.0", + "version": "1.0.1", "type": "package", "authors": [ { diff --git a/src/Date.php b/src/Date.php index ccc357c..ce08e34 100644 --- a/src/Date.php +++ b/src/Date.php @@ -2,8 +2,8 @@ namespace Simples\Helper; -use DateTime; use DateInterval; +use DateTime; /** * Class Date @@ -71,7 +71,28 @@ public static function isDate($date) */ public static function nextMonth(string $date, int $months): string { - return date('Y-m-d', strtotime($date . " +" . $months . " month")); + $base = DateTime::createFromFormat(static::$format, $date); + $year = $base->format('Y'); + $month = $base->format('n'); + $day = $base->format('d'); + + $year += floor($months / 12); + $months = $months % 12; + $month += $months; + if ($month > 12) { + $year++; + $month = $month % 12; + if ($month === 0) { + $month = 12; + } + } + + $next = DateTime::createFromFormat('Y-m-d', $year . '-' . $month . '-' . $day); + if (!checkdate($month, $day, $year)) { + $next = DateTime::createFromFormat('Y-m-j', $year . '-' . $month . '-1'); + $next->modify('last day of'); + } + return $next->format(static::$format); } /**