note 103196 deleted from function.date by tularis
| From: | tularis@php.net | Date: | Thu, 09 Feb 2012 20:51:19 +0000 |
| Subject: | note 103196 deleted from function.date by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-185958@lists.php.net to get a copy of this message | ||
Note Submitter: scorp at scorp dot ca
----
Spend lot of time to find correct function to calculate next invoice (billing) date, but didn't
find. Make new one.
Condition:
Source date: 2011-01-31
Recurring payments each month at same day or early if source day more than end of month (31 is more
than 28 Feb for example).
Here is results:
<?php
function get_next_invoice_date($source_date, $last_date)
{
if (is_null($last_date) || ($last_date == ''))
{
return $source_date;
}
$srctime = strtotime($source_date);
$lasttime = strtotime($last_date);
$source_day = date("d", $srctime);
$next_month = date("Y-m", strtotime( date("Ym", $lasttime) . '01 +1
month' ));
$next_month_time = strtotime($next_month);
$next_month_last_day = date("d", strtotime("-1 second", strtotime("+2
month", strtotime( date("Y", $lasttime) . date("m", $lasttime) .
"01" ))));
if ($source_day > $next_month_last_day)
{
$new_day = $next_month_last_day;
}
else
{
$new_day = $source_day;
}
$next_invoice_date = $next_month . '-' . $new_day;
return $next_invoice_date;
} // get_next_invoice_date
// Test:
$first_invoice_date = '2011-01-31';
$last_invoice_date = '';
$list = array();
for ($i = 1; $i <= 24; $i++)
{
$last_invoice_date = get_next_invoice_date($first_invoice_date, $last_invoice_date);
$list[] = $last_invoice_date;
}
print_r($list);
?>