note 80052 added to function.mktime

From: Date: Thu, 27 Dec 2007 08:39:59 +0000
Subject: note 80052 added to function.mktime
Groups: php.notes 
Request: Send a blank email to php-notes+get-134028@lists.php.net to get a copy of this message
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 ---- Server IP: 203.199.69.133 Probable Submitter: 59.96.185.221 ---- Manual Page -- http://www.php.net/manual/en/function.mktime.php Edit -- https://master.php.net/note/edit/80052 Del: integrated -- https://master.php.net/note/delete/80052/integrated Del: useless -- https://master.php.net/note/delete/80052/useless Del: bad code -- https://master.php.net/note/delete/80052/bad+code Del: spam -- https://master.php.net/note/delete/80052/spam Del: non-english -- https://master.php.net/note/delete/80052/non-english Del: in docs -- https://master.php.net/note/delete/80052/in+docs Del: other reasons-- https://master.php.net/note/delete/80052 Reject -- https://master.php.net/note/reject/80052 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#134028) next »