note 95769 added to function.strtotime

From: Date: Thu, 21 Jan 2010 03:41:29 +0000
Subject: note 95769 added to function.strtotime
Groups: php.notes 
Request: Send a blank email to php-notes+get-165155@lists.php.net to get a copy of this message
Previous "Next Month" fix/function only worked for when the user wanted the following month. This function should work for any number of desired month(s) either before or after the start date. <?php /////////////////////////////////////////////////////// //$fromdate = The start date used to calculate the month's before or after. (ie. For "January 20th, 2010" you would use 'strtotime("January 20 2010")'). //$months = The number of months (ie. For "Next Month", you would use the number "1". For "Two Months Before", you would use the number "2". //$beforeafter = indicator of whether you want the number of months before or after the startdate. (ie. For "Before" you would use "-" sign or the word "before"). // EXAMPLE: To find out the next month date from January 31th, 2010, you would use the Syntax: findTrueMonth(strtotime("January 31 2010")) /////////////////////////////// function findTrueNextMonth($fromdate = null,$months = "1", $beforeafter = "+") { if(!$fromdate){$fromdate=strtotime("now");} $testDay = date('d', $fromdate); $beforeafter = strtolower($beforeafter); //Find out if the user wants us to return the month(s) before or after the start date switch ($beforeafter) { case "-": case "before": $beforeafter="-".strval($months)." month"; break; default: $beforeafter="+".strval($months)." month"; break; } //Normal syntax for calculating the timestamp $maybeDate = strtotime("{$beforeafter}", $fromdate); //Only correct the returned date if it is not the same day of the month as the start date if (date('d', $maybeDate)!=$testDay){ $month = date('m', $maybeDate)-1; $year = date('Y', $maybeDate); //Find the first day of the incorrect month and return the last day of the previous month $trueDate = strtotime("{$year}-{$month}-01"); $trueDate = strtotime('-1 second', strtotime('+1 month', $trueDate)); return $trueDate; }else{ return $maybeDate; } } //TESTS/RESULTS - $default_Month = findTrueNextMonth(); //No Parameters will return Next Month's date from today echo date("m-d-Y",$default_Month); //Output: 02-20-2010 $next_Month = findTrueNextMonth(strtotime("January 31 2010")); //Single Parameter will return Next Month's date from date entered echo date("m-d-Y",$next_Month); //Output: 02-28-2010 $eighteen_Month_Before = findTrueNextMonth(strtotime("January 31 2010"),19, "before"); //19 Months Before Date Entered echo date("m-d-Y",$eighteen_Month_Before); //Output: 06-30-2008 /////////////////////////////// ?> *NOTE: Possible problems may arise if the user is attempting to calculate more then about 25 months either ahead or behind the start date. This error is due to the system using a default value of 31 days per month, leaving an accumulating remainder of days for months with less then 31 days. Eventually that remainder may exceed the amount of day's in the system's return month resulting in it returning a month 2 months ahead of the anticipated returned month. ---- Server IP: 69.147.83.197 Probable Submitter: 124.168.1.80 ---- Manual Page -- http://www.php.net/manual/en/function.strtotime.php Edit -- https://master.php.net/note/edit/95769 Del: integrated -- https://master.php.net/note/delete/95769/integrated Del: useless -- https://master.php.net/note/delete/95769/useless Del: bad code -- https://master.php.net/note/delete/95769/bad+code Del: spam -- https://master.php.net/note/delete/95769/spam Del: non-english -- https://master.php.net/note/delete/95769/non-english Del: in docs -- https://master.php.net/note/delete/95769/in+docs Del: other reasons-- https://master.php.net/note/delete/95769 Reject -- https://master.php.net/note/reject/95769 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#165155) next »