note 35359 deleted from function.date by sniper

From: Date: Sat, 11 Oct 2003 16:55:33 +0000
Subject: note 35359 deleted from function.date by sniper
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-58304@lists.php.net to get a copy of this message
Note Submitter: SARGe @ poopshovel.com ---- There has to be an easier way. I needed this so we could pick any date and then find a date in the same format 7 or 14 days prior. Then use WHERE Date BETWEEN '$startdate' AND '$rdate' for a weekly report. This only works up to 30 days. function datecalc($rdate, $j=7) { /*Take 2003-08-27 and split it into 3 variables $y $m $d */ $rdate = explode("-",$rdate); $y = $rdate[0]; $m = $rdate[1]; $d = $rdate[2]; ## CHECK LEAP YEAR if($y == '2004' || $y == '2008' || $y == '2012'){ $leap='Y'; } /* MONTH SWITCH: Determins days in previous month $dm */ switch ($m) { case 2: $dm = '31'; break; case 3: if($leap == 'Y') { $dm = '29'; break; }else{ $dm '28'; break; } case 4: $dm = '31'; break; case 5: $dm = '30'; break; case 6: $dm = '31'; break; case 7: $dm = '30'; break; case 8: $dm = '31'; break; case 9: $dm = '31'; break; case 10: $dm = '30'; break; case 11: $dm = '31'; break; case 12: $dm = '30'; break; case 1: $dm = '31'; break; } ## END SWITCH ## SUBTRACT DAYS AND ROLL BACK MONTHS ## $rd = $d;$rm = $m;$ry = $y;$rj = $j; while($rj >= 1) { $rd = $rd - 1; //Subtract 1 day $rj = $rj - 1; //Subtract 1 itteration if($rd == '1') //Check for first day of month { $rm = $rm - 1; //Back one month $rd = $dm; //Set day equal to days of previous month } if($rm < 1) //Sets up previous year { $ry = $ry - 1; //Subtracts year $rm = '12'; //We know this must be December $rd = '31'; //December has 31 days } } ## CREATE THE NEW DATE $rdate ## $rdate = $ry; $rdate .= '-'; $rdate .= $rm; $rdate .= '-'; $rdate .= $rd; return $rdate; } ## END DATECALC ## $rdate='2003-08-27'; ## datecalc($rdate, 14)

« previous php.notes (#58304) next »