Hello Everyone,

I am trying to compute the subscription next due date.
For example, these are the subscription dates:
1/28/13
1/29/13
1/30/13
1/31/13

When I used the php function strtotime( "+1 month", <date> ) the next month due date will be:
1/28/13 due date is 2/28/13
1/29/13 due date is 3/1/13
1/30/13 due date is 3/2/13
1/31/13 due date is 3/3/13

With this, it will change the due date of the subscriber even though other months have the 29th, 30th and 31st. Can you please provide any suggestions or options on how to handle this?

Thanks in advance

Try this

<?php

    $dates = array("1/28/13","1/29/13","1/30/13","1/31/13");
    foreach($dates as $myDate) {
        $dueDate = date('n/j/y', strtotime("+1 months", strtotime($myDate)));
        $myDateMonth = date ('n',strtotime($myDate));
        $dueDateMonth = date('n', strtotime($dueDate));
        $myDateYear = date ('Y',strtotime($myDate));
        $dueDateYear = date ('Y',strtotime($dueDate));


            if(($dueDateMonth - $myDateMonth)>1) {
                if($myDateYear==$dueDateYear) $nextMonth = $myDateMonth+1;
                else if($myDateYear<$dueDateYear) $nextMonth = 1;
                $dueDateOrg = date("n/t/y", strtotime($nextMonth."/1/".$dueDateYear));
            }
            else {
                $dueDateOrg = $dueDate;
            }



        echo $myDate." due date is ".$dueDateOrg."<br />";
    }

?>
Member Avatar for diafol

So what do you want them to be? Stop month overflow, so that 'overflowed' months return the last day of the month, e.g. 31/1/2013 -> 28/2/2013

You can keep find the number of days in the month and work from there.

$startDate = "2013-12-31";
$monthsToAdvance=2;


function getDates($startDate, $monthsToAdvance)
{
    $dt = new DateTime($startDate);
    $day = $dt->format('d');
    $dt->setDate($dt->format('Y'),$dt->format('n'),1);

    $dt->add(new DateInterval('P'.$monthsToAdvance.'M'));
    $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $dt->format('n'), $dt->format('Y'));
    if($day > $daysInMonth) $day = $daysInMonth;
    $dt->setDate($dt->format('Y'),$dt->format('n'),$day);   
    return $dt->format('Y-m-d');
}

echo getDates($startDate, $monthsToAdvance);

Maybe? Or you can use the 't' instead of the cal function...

function getDates($startDate, $monthsToAdvance)
{
    $dt = new DateTime($startDate);
    $day = $dt->format('d');
    $dt->setDate($dt->format('Y'),$dt->format('n'),1);

    $dt->add(new DateInterval('P'.$monthsToAdvance.'M'));
    $daysInMonth = $dt->format('t');
    if($day > $daysInMonth) $day = $daysInMonth;
    $dt->setDate($dt->format('Y'),$dt->format('n'),$day);   
    return $dt->format('Y-m-d');
}
commented: Interesting workarround +11

Thanks for the reply.

Yes, I would like to stop the overflow if the due date is every last day of a month and to check if it exceeds to the next month.

I tried the following approaches you provided and it works well.

Thank you Bachov and Diafol

Member Avatar for diafol

Ok, solved?

Yes. I believe the 2nd function you provided will do the work. Thanks. Will mark this as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.