note 80052 deleted from function.mktime by derick
| From: | derick@php.net | Date: | Sat, 26 Jul 2008 10:06:21 +0000 |
| Subject: | note 80052 deleted from function.mktime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142774@lists.php.net to get a copy of this message | ||
Note Submitter: hiten dot barchha at gmail dot com
----
There was one requirement of finding recurring date for daily, monthly, quarterly and yearly
subscription.
Following is the example to find out next recurring dates.
Logic : Code is taking care of following things.
1. Date is valid date
2. If next recurring date is not valid than it will take last date of month.
3. This code will useful to those who are interested to keep track of recurring payments on his own
database
rather than payment gateway.
4. Code will allow you to find dates for daily, monthly, quarterly and yearly.
<?PHP
function getDates($bdate,$duration) {
$aDate = explode("-",$bdate);
$datetime = mktime(0, 0, 0, $aDate[1], $aDate[2], $aDate[0]);
switch($duration) {
case 0:
for($i=0;$i<12;$i++) {
$nextmonth = date("Y-m-d",mktime(0, 0, 0, date("m",$datetime),
date("d",$datetime)+1+$i, date("Y",$datetime)));
echo $nextmonth."<br><br>";
}
break;
case 1:
for($i=0;$i<12;$i++) {
$tempnextmonth = ($aDate[1]+1+$i)%12;
if($tempnextmonth == 0) {
$tempnextmonth = 12;
}
$nextyear = date("Y",mktime(0, 0, 0, date("m",$datetime)+1+$i,
date("d",$datetime), date("Y",$datetime)));
if (checkdate(str_pad($tempnextmonth,2,0,STR_PAD_LEFT),
$aDate[2],$nextyear) == false) {
echo $nextyear."-".str_pad($tempnextmonth,2,0,STR_PAD_LEFT)."-".
date('d', mktime(0, 0, 0, ($tempnextmonth + 1), 0, $nextyear))
."<br><br>";
}
else {
echo $nextyear."-".str_pad($tempnextmonth,2,0,STR_PAD_LEFT)."-".
$aDate[2]."<br><br>";
}
}
break;
case 2:
for($i=0;$i<12;$i++) {
$tempnextmonth = ($aDate[1]+4+(4*$i))%12;
if($tempnextmonth == 0) {
$tempnextmonth = 12;
}
$nextyear = date("Y",mktime(0, 0, 0, date("m",$datetime)+4+(4*$i),
date("d",$datetime), date("Y",$datetime)));
if (checkdate(str_pad($tempnextmonth,2,0,STR_PAD_LEFT),
$aDate[2],$nextyear) == false) {
echo $nextyear."-".str_pad($tempnextmonth,2,0,STR_PAD_LEFT)."-".
date('d', mktime(0, 0, 0, ($tempnextmonth + 1), 0, $nextyear)).
"<br><br>";
}
else {
echo $nextyear."-".str_pad($tempnextmonth,2,0,STR_PAD_LEFT)."-".
$aDate[2]."<br><br>";
}
}
break;
case 3:
for($i=0;$i<12;$i++) {
$nextmonth = date("Y-m-d",mktime(0, 0, 0, date("m",$datetime),
date("d",$datetime), date("Y",$datetime)+1+$i));
echo $nextmonth."<br><br>";
}
break;
}
}
?>
How to use this function?
<?PHP getDates(date("Y-m-d"),$period); ?>
You can set $period as your recurring period time.
0 - Daily Dates
1 - Monthly Dates
2 - Quarterly Dates
3 - Yearly Dates