PHP Next Month Same Date
2 min readJan 2, 2019
PHP does not have a built-in next month same date function.But many times for payment charges we need this.This may be a salary date, or a monthly subscription date.
If today is Dec 31 2018, The next (n) dates will be
Jan 31 2019,
Feb 28 2019,
Mar 31 2019,
Apr 30 2019,
………………..
………………..
and so on.
It also handles leap years perfectly
If dates starts on Feb 29 2020 next (n) dates will be
Mar 29 2020,
Apr 29 2020,
………………..
………………..
Feb 28 2021,
Mar 29 2021,
Apr 29 2021,
………………..
………………..
Feb 29 2024
and so on.
(2100 is not a leap year, it also handles every 100th non-leap year)
<?php
$dates = Hridate::next_months_same_date("2099-12-31",72;)
echo "<pre>";
print_r($dates);
Class Hridate {
public function test_next_months_same_date()
{
//test it
$given_date_string = "2099-12-31";
$got = $this->next_months_same_date($given_date_string, $month_count = 72);
echo "<pre>";
print_r($got);
}
public function test_next_month_same_date()
{
$given_date_string = "2100-01-31";
$got = $this->next_month_same_date($given_date_string);
echo $got;
}
public function next_months_same_date($given_datestring, $month_count)
{
$next_months_arr = array();
$next = $given_datestring;
for ($i = 0; $i < $month_count; $i++) {
$next = $this->next_month_same_date($next, $given_datestring);
$next_months_arr[] = $next;
}
return $next_months_arr;
}
public function next_month_same_date($given_date_string, $initial_date_string)
{
$exploded_given_date_string = explode("-", $given_date_string);
$exploded_initial_date_string = explode("-", $initial_date_string);
$given_year_string = $exploded_given_date_string[0];
$given_month_string = $exploded_given_date_string[1];
$given_date_string = $exploded_given_date_string[2];
$initial_year_string = $exploded_initial_date_string[0];
$initial_month_string = $exploded_initial_date_string[1];
$initial_date_string = $exploded_initial_date_string[2];
$next_month = array();
$next_month["01"] = "02";
$next_month["02"] = "03";
$next_month["03"] = "04";
$next_month["04"] = "05";
$next_month["05"] = "06";
$next_month["06"] = "07";
$next_month["07"] = "08";
$next_month["08"] = "09";
$next_month["09"] = "10";
$next_month["10"] = "11";
$next_month["11"] = "12";
$next_month["12"] = "01";
$output_month_string = $next_month[$given_month_string];
$get_output_year = function () use ($given_year_string, $output_month_string) {
$given_year_string = (int)$given_year_string;
$year_string = $output_month_string == "01" ? $given_year_string + 1 : $given_year_string;
return (string)$year_string;
};
$output_year_string = $get_output_year();
$is_output_year_leap_year = $this->isLeapYear($output_year_string);
$get_output_date = function () use ($output_month_string, $initial_date_string, $is_output_year_leap_year) {
$days_in_month = array();
$days_in_month["01"] = "31";
$days_in_month["02"] = $is_output_year_leap_year ? "29" : "28";
$days_in_month["03"] = "31";
$days_in_month["04"] = "30";
$days_in_month["05"] = "31";
$days_in_month["06"] = "30";
$days_in_month["07"] = "31";
$days_in_month["08"] = "31";
$days_in_month["09"] = "30";
$days_in_month["10"] = "31";
$days_in_month["11"] = "30";
$days_in_month["12"] = "31";
return $initial_date_string > (int)$days_in_month[$output_month_string] ? $days_in_month[$output_month_string] : $initial_date_string;
};///jj
$output_date_string = $get_output_date();
return "$output_year_string-$output_month_string-$output_date_string";
}
public function isLeapYear($year)
{
$year = (int)$year;
return $year % 4 == 0 && !($year % 100 == 0);
}
}