note 63588 added to function.strtotime

From: Date: Fri, 24 Mar 2006 17:27:33 +0000
Subject: note 63588 added to function.strtotime
Groups: php.notes 
Request: Send a blank email to php-notes+get-106535@lists.php.net to get a copy of this message
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; } ---- Server IP: 64.71.164.2 Probable Submitter: 24.234.149.245 ---- X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER autolearn=no version=2.64 ---- Manual Page -- http://www.php.net/manual/en/function.strtotime.php Edit -- http://master.php.net/note/edit/63588 Del: integrated -- http://master.php.net/note/delete/63588/integrated Del: useless -- http://master.php.net/note/delete/63588/useless Del: bad code -- http://master.php.net/note/delete/63588/bad+code Del: spam -- http://master.php.net/note/delete/63588/spam Del: non-english -- http://master.php.net/note/delete/63588/non-english Del: in docs -- http://master.php.net/note/delete/63588/in+docs Del: other reasons-- http://master.php.net/note/delete/63588 Reject -- http://master.php.net/note/reject/63588 Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#106535) next »