A Class for Validating and Formatting Dates
By Tony Marston2005-04-16
Incrementing or Decrementing a Date
There may come a time when you need to find a date which is several days before or after a given date. This can be done by using the GregoriantoJD() function to convert a Gregorian date to a Julian date (the number of days since a base date, which is 4714 BC in this case), add or subtract from the Julian day count, then use the JDtoGregorian() function to convert from Julian back into Gregorian. This can be done by adding another function to our date class. In the following code you will notice that the new addDays() function makes use of the getInternalDate() function to process the input and the getExternalDate() function to format the output.
function addDays ($internaldate, $days)
// add a number of days (may be negative) to $internaldate (YYYY-MM-DD)
// and return the result in the same format
{
// ensure date is in internal format
$internaldate = $this->getInternalDate($internaldate);
// convert to the number of days since basedate (4714 BC)
$julian = GregoriantoJD(substr($internaldate,5,2)
,substr($internaldate,8,2)
,substr($internaldate,0,4));
$days = (int)$days;
$julian = $julian + $days;
// convert from Julian to Gregorian (format m/d/y)
$gregorian = JDtoGregorian($julian);
// split date into its component parts
list ($month, $day, $year) = split ('[/]', $gregorian);
// convert back into standard format
$result = $this->getInternaldate("$day/$month/$year");
return $result;
Tutorial Pages:
» A Class for Validating and Formatting Dates
» Defining the Class to Handle Dates
» Display date to the User
» Incrementing or Decrementing a Date
» Using this Class in Your Code
» Summary
