add and sub month

This commit is contained in:
Nasser Hekmati committed 2017-05-08 23:43:37 +04:30
1 parent 04993d769b
commit 46b8025027
1 file changed
+77 -9
+77 -9
View File
@@ -940,6 +940,83 @@ class Verta extends DateTime {
return $this->subYears($value); return $this->subYears($value);
} }
/**
* Add months to the instance. Positive $value travels forward while
* negative $value travels into the past.
*
* @param int $value
*
* @return static
*/
public function addMonths($value)
{
list($year, $month, $day, $hour, $minute, $second) = explode('-', $this->format('Y-m-d-H-i-s'));
$month += $value;
if ($month > 12) {
$year += intval($month / 12);
$month = $month % 12;
}
if ($month < 1) {
$year += intval($month / 12) - 1;
$month = 12 + ($month % 12);
}
if ($value > 0) {
if ($month > 6 && $day == 31) {
$day--;
}
if ($month == 12 && !self::isLeapYear($year)) {
$day--;
}
}
else {
if ($month == 12 && $day == 31) {
if (self::isLeapYear($year)) {
$day = 30;
}
else {
$day = 29;
}
}
}
return self::create($year, $month, $day, $hour, $minute, $second, $this->getTimeZone());
}
/**
* Add a month to the instance
*
* @param int $value
*
* @return static
*/
public function addMonth($value = 1)
{
return $this->addMonths($value);
}
/**
* Remove a month from the instance
*
* @param int $value
*
* @return static
*/
public function subMonth($value = 1)
{
return $this->subMonths($value);
}
/**
* Remove months from the instance
*
* @param int $value
*
* @return static
*/
public function subMonths($value)
{
return $this->addMonths(-1 * $value);
}
/** /**
* Add days to the instance. Positive $value travels forward while * Add days to the instance. Positive $value travels forward while
* negative $value travels into the past. * negative $value travels into the past.
@@ -989,15 +1066,6 @@ class Verta extends DateTime {
return $this->addDays(-1 * $value); return $this->addDays(-1 * $value);
} }
/**
* Add weekdays to the instance. Positive $value travels forward while
* negative $value travels into the past.
*
* @param int $value
*
* @return static
*/
/** /**
* Add weeks to the instance. Positive $value travels forward while * Add weeks to the instance. Positive $value travels forward while