0

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

3
Contributors
5
Replies
40
Views
3 Years
Discussion Span
Last Post by jamojo
1

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 />";
    }

?>
2

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');
}

Edited by diafol

Votes + Comments
Interesting workarround
0

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

0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.