note 87858 added to function.strtotime

From: Date: Sat, 27 Dec 2008 05:59:23 +0000
Subject: note 87858 added to function.strtotime
Groups: php.notes 
Request: Send a blank email to php-notes+get-148555@lists.php.net to get a copy of this message
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))); } ?> ---- Server IP: 208.69.120.35 Probable Submitter: 68.18.76.90 ---- Manual Page -- http://www.php.net/manual/en/function.strtotime.php Edit -- https://master.php.net/note/edit/87858 Del: integrated -- https://master.php.net/note/delete/87858/integrated Del: useless -- https://master.php.net/note/delete/87858/useless Del: bad code -- https://master.php.net/note/delete/87858/bad+code Del: spam -- https://master.php.net/note/delete/87858/spam Del: non-english -- https://master.php.net/note/delete/87858/non-english Del: in docs -- https://master.php.net/note/delete/87858/in+docs Del: other reasons-- https://master.php.net/note/delete/87858 Reject -- https://master.php.net/note/reject/87858 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#148555) next »