note 63588 deleted from function.strtotime by derick

From: Date: Sun, 08 Apr 2007 13:04:28 +0000
Subject: note 63588 deleted from function.strtotime by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-125215@lists.php.net to get a copy of this message
Note Submitter: logistiker ---- strtotime is a great function assuming you use 64-bit libraries. If you don't and you try to use dates outside the 32-bit timestamp, then it's totally useless. Here's a function that can manipulate years, months and days for an actual date: function dateAdd($dateValue, $number = 1, $datePart = 'd', $inputDateFormat = 'ymd', &$parsedDate = false){ //return false if date value is not a date if (! isDate($dateValue, $inputDateFormat, $arrDate)) return false; //if $number is a decimal, just round to the nearest integer if (! is_int($number)) $number = round($number); $currentYear = $arrDate['year']; $currentMonth = $arrDate['month']; $currentDay = $arrDate['day']; switch ($datePart){ case 'y': $currentYear = $arrDate['year'] + $number; break; case 'm': $currentYear += ($number < 0) ? ceil($number / 12) : floor($number / 12); $currentMonth += $number % 12; if ($currentMonth <= 0) $currentMonth += 12; if ($currentMonth > 12){ $currentMonth -= 12; $currentYear++; } //Convert the year to 4 digits so the isDate function will validate the date (only validates 4 digit years) $currentYear = toIsoYear($currentYear); //If the day doesn't exist in the next month then decrement to the last day of the month while (! isDate("$currentYear-$currentMonth-$currentDay")) $currentDay--; break; case 'd': $numLoopDays = abs($number); for ($i = 0; $i < $numLoopDays; $i++){ $currentDay = ($number >= 0) ? $currentDay + 1 : $currentDay - 1; if ($currentDay > daysInMonth($currentMonth, $currentYear) || $currentDay < 1){ $currentMonth = ($number >= 0) ? $currentMonth + 1 : $currentMonth - 1; if ($currentMonth == 13){ $currentMonth = 1; $currentYear++; } if ($currentMonth == 0){ $currentMonth = 12; $currentYear--; } $currentDay = ($currentDay < 1) ? daysInMonth($currentMonth, $currentYear) : 1; } } break; } //iso 8601 requires zero padding for years between -999 and 999 $currentYear = toIsoYear($currentYear); //iso 8601 requires zero padding for months & days < 10 $currentMonth = toIsoMonthDay($currentMonth); $currentDay = toIsoMonthDay($currentDay); //parsed date $parsedDate['year'] = $currentYear; $parsedDate['month'] = $currentMonth; $parsedDate['day'] = $currentDay; //iso 8601 date output $newDate = "$currentYear-$currentMonth-$currentDay"; return $newDate; }

« previous php.notes (#125215) next »