I have two columns in my db: start_date
and end_date
, which are both DATE
types. My code is updating the dates as follows:
I know - sort of late. But I was working at the same problem. If a client buys a month of service, he/she expects to end it a month later. Here's how I solved it:
$now = time();
$day = date('j',$now);
$year = date('o',$now);
$month = date('n',$now);
$hour = date('G');
$minute = date('i');
$month += $count;
if ($month > 12) {
$month -= 12;
$year++;
}
$work = strtotime($year . "-" . $month . "-01");
$avail = date('t',$work);
if ($day > $avail)
$day = $avail;
$stamp = strtotime($year . "-" . $month . "-" . $day . " " . $hour . ":" . $minute);
This will calculate the exact day n*count months from now (where count <= 12). If the service started March 31, 2019 and runs for 11 months, it will end on Feb 29, 2020. If it runs for just one month, the end date is Apr 30, 2019.