Dear sir/mam,
hello!
This is Pradeep Kumar from India. I felt the PHP manual provided by you,
highly helpful for development. Being a regular user of manual I feel it my duty
to inform you about any bug I encounter in the manual.
In date() function reference page the first function under the heading
User contributed notes: the first function datecalc($rdate, $j=7) returns date
with subtracted $j days from $rdate gives wrong output for any $j one less then
day of the date $rdate (eg. dateecalc('17-04-2004',16 )) or any no of days more than this.
This is because of a wrong condition check in the code.
The right code with enhanced leap year check is :


//====================================
function datecalc ($rdate, $j=7)
{
        $rdate = explode("-",$rdate);
        $y = $rdate[0];  $m = $rdate[1];  $d = $rdate[2];
// CHECK LEAP YEAR
if($y%4==0 && ($y%100!=0 || $y%400==0)) { $leap='Y'; }
// MONTH SWITCH: Determines 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) {
   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
   }
   else   $rd = $rd - 1; //Subtract 1 day
   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
   }
   $rj = $rj - 1; //Subtract 1 iteration
}
// CREATE THE NEW DATE $rdate
$rdate = $ry;
$rdate .= '-';
$rdate .= $rm;
$rdate .= '-';
$rdate .= $rd;
return $rdate;
} // END datecalc()

//====================================

Hope this right code could be helpful for some other programmers like me.

_________________________________________________________________
Join BharatMatrimony.com. http://www.bharatmatrimony.com/cgi-bin/bmclicks1.cgi?72 Unmarried? Join Free.

Reply via email to