Skip to content

Commit

Permalink
1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
wilcorrea committed Mar 26, 2017
1 parent e6060b4 commit 75e27e1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
],
"homepage": "https://github.com/phpzm/helper",
"license": "MIT",
"version": "1.0.0",
"version": "1.0.1",
"type": "package",
"authors": [
{
Expand Down
25 changes: 23 additions & 2 deletions src/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Simples\Helper;

use DateTime;
use DateInterval;
use DateTime;

/**
* Class Date
Expand Down Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 75e27e1

Please sign in to comment.