note 87858 deleted from function.strtotime by danbrown

From: Date: Sat, 27 Dec 2008 17:45:26 +0000
Subject: note 87858 deleted from function.strtotime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148579@lists.php.net to get a copy of this message
Note Submitter: wck555 at gmailgmail dot dot comcom ---- Bug #44073 illustrates a difference of opinion on what "+1 month" means. http://bugs.php.net/bug.php?id=44073 In my application, I need to use tuxedobob's interpretation of "+1 month". That is: "3/31/2008 + 1 month" = "4/30/2008" (not "5/1/2008"). Below is a function AddMonths() that adds months to a give date using this interpretation. There is probably a simpler way to do this, but I need many of the intermediate functions in other places. <? // AddMonths() // // This function adds the given number of months to $dtDate. If the resulting // date is beyond the end of the resulting month (ex: 1/31/2000 + 1 month = 2/31/2001 // which is invalid), the resulting date is rolled back to the last day of the // month (ex: 1/31/2000 + 1 month = 2/28/2001) // // PARAMETERS: // $dtDate - starting date // $months - number of months to add // // RETURN: (DateTime) $dtDate + $months function AddMonths($dtDate, $months) { $numericDate = iDateToNumeric($dtDate) + $months; $month = ($numericDate % 12) + 1; $year = (floor($numericDate/12)); $day = min($dtDate->format("j"), iDaysInMonth($month, $year)); return(new DateTime("$month/$day/$year")); } // iDaysInMonth() // // This function determines the number of days in a given month, year // // PARAMETERS: // $month - The month // $year - The year // // RETURN: Number of days in given month/year Function iDaysInMonth($month, $year) { switch($month) { case 4: case 6: case 9: case 11: return(30); break; case 2: // --- February - need to check for leapyear if(IsLeapYear($year)) { return(29); } else { return(28); } break; default: return(31); break; } } // IsLeapYear() // // This function determines if the specified year is a leap year // // PARAMETERS: // $year - the year // // RETURN: True of $year is a leap year Function IsLeapYear($year) { if ($year % 4 != 0) { return false; } else { if ($year % 100 != 0) { return true; } else { if ($year % 400 != 0) { return false; } else { return true; } } } } // iDateToNumeric() // // This function converts a date to an integer numeric date value. The value // returned by this function is the number of months since 0 AD. // // PARAMETERS: // Date dtDate - date to be converted // // RETURN: numeric date value function iDateToNumeric($dtDate) { return($dtDate->format("Y")*12 + $dtDate->format("n") - 1); } // dtNumericToDate() // // This function converts a numeric date value back to a standard date. // // PARAMETERS: // Date iNumeric - numeric date to be converted // // RETURN: date value function dtNumericToDate($iNumeric) { return(new DateTime(($iNumeric%12) + 1 . "/1/" . floor($iNumeric/12))); } ?>

« previous php.notes (#148579) next »