note 103196 added to function.date
| From: | scorp at scorp dot ca | Date: | Thu, 31 Mar 2011 03:18:25 +0000 |
| Subject: | note 103196 added to function.date | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-178368@lists.php.net to get a copy of this message | ||
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);
?>
----
Server IP: 216.235.13.26
Probable Submitter: 99.226.77.121
----
Manual Page -- http://www.php.net/manual/en/function.date.php
Edit -- https://master.php.net/note/edit/103196
Del: integrated -- https://master.php.net/note/delete/103196/integrated
Del: useless -- https://master.php.net/note/delete/103196/useless
Del: bad code -- https://master.php.net/note/delete/103196/bad+code
Del: spam -- https://master.php.net/note/delete/103196/spam
Del: non-english -- https://master.php.net/note/delete/103196/non-english
Del: in docs -- https://master.php.net/note/delete/103196/in+docs
Del: other reasons-- https://master.php.net/note/delete/103196
Reject -- https://master.php.net/note/reject/103196
Search -- https://master.php.net/manage/user-notes.php