#native_company# #native_desc#
#native_cta#

DateObj – date arithmetic class

By Yaron Yogev
on October 2, 2001

Version: 1.1.1.2

Type: Class

Category: Calendars/Dates

License: GNU General Public License

Description: This class can be set by timestamp or year,month&day.
Methods include:
* set methods as described above
* get methods for year, month, day of month
* equals( <other DateObj> ) – checks if the the other nstance has the same date
* getNextDay() – retuens a DateObj instance with the date of the following day
* getNextMonth(), getNextYear() – returns a DateObj instance with the date a month after this one or a year after this one
* diff( <other DateObj> [, <unit>] ) – returns the difference between the timestamps of this instance and the other, measured in the given unit.
* toString( [<format>] ) – returns a string represting the date it has. uses the date() function to format this string.

<?php
/*
file: dateUtils.php

Author: Yaron Yogev ([email protected])

17/01/2001  Yaron Yogev
	    File Created
*/

// constants for date arithmetic using Unix timestamp
// time is measured in seconds since the Epoch
$oneMinute = 60 ;
$oneHour = 60 * $oneMinute ;
$oneDay = 24 * $oneHour ;
$oneYear = 365.25 * $oneDay ;

/*
Class: DateObj

Description: 
    - Holds a represntation of a date.
    - Allows setting the date by year, month and date or by timestamp.
    - Provides methods for getting the parts of the date in the instance
	and for getting the date one day, one month or one year after it.

Methods:
    * constructor - DateObj()
	- input: NONE
	- side effects: sets this instance to the current system time

    * setDate( $year, $month, $day )
	- sets the date contained in this instance to the specified values

    * setTimestamp( $newTimestamp )
	- sets the date in this instance using the specified Unix timestamp

    * getYear(), getMonth(), getDayOfMonth()
	- return the numeric values of the date represented in this instance

    * equals( $other )
	- comapres the year, month and date with another DateObj instance
	- if all are equal, returns true, otherwise returns false

    * getNextDay()
	- returns the date following the one in this instance

    * getNextMonth()
	- returns the date one month after the date i this instance
	- if the day of month for this instance is bigger than 
	    the number of days in the next month,
	    it returns the last day of the next month.

    * getNextYear()
	- returns the date one year from now
	- if this is a leap year and we're on February 29th, returns March 1st

    * diff( $other [, $unit ] )
	- returns the difference between the dates in this instance and $other
	- the value is not rounded, e.g. you may get a return value of 2.812.
	    so manipulate it later it as needed.
	- valid units are: "year", "day", "hour", "minute", "seconds"
	- default unit is "day"
	- if $other is later, the result will be negative

    * toString( $format )
	- returns a string representing the date in the instance,
	    as created by the date() function using the given format

    * echoData()
	- for debugging: prints an HTML remark with the data of the instance
*/
class DateObj
{
    var $timestamp ;
    var $dateArray ;

    // constructor - uses current time
    function DateObj()
    {
	$this->setTimestamp( time() ) ;
    } // end constructor

    // set date - allows to set the date stored in this DateObj
    function setDate( $year, $month, $day )
    {
	$this->setTimestamp( mktime(
	    0, // hour
	    0, // minute
	    0, // second
	    $month,
	    $day,
	    $year ) ) ;
    } // end function setDate

    function setDateFromDb( $dateVal )
    {
	// parse a date value that was read through ODBC
	$parts = explode( " ", $dateVal ) ;
	$datePart = $parts[0] ;
	$timePart = $parts[1] ;
	$dateParts = explode( "-", $datePart ) ;
	$timeParts = explode( ":", $timePart ) ;
	$year = $dateParts[0] ;
	$month = $dateParts[1] ;
	$day = $dateParts[2] ;
	$hour = $timeParts[0] ;
	$minute = $timeParts[1] ;
	$second = $timeParts[2] ;
	$this->setTimestamp( mktime(
	    $hour,
	    $minute,
	    $second,
	    $month,
	    $day,
	    $year ) ) ;
    } // end function setDateFromDb

    // set the date according to a timestamp
    function setTimestamp( $newTimestamp )
    {
	$this->timestamp = $newTimestamp ;
	$this->dateArray= getdate( $this->timestamp ) ;
    }

    // "get" methods
    function getYear()
    {
	return $this->dateArray["year"] ;
    }
    function getMonth()
    {
	return $this->dateArray["mon"] ;
    }
    function getDayOfMonth()
    {
	return $this->dateArray["mday"] ;
    }

    function equals( $other )
    {
	if (	( $this->getYear() == $other->getYear() ) &&
		( $this->getMonth() == $other->getMonth() ) &&
		( $this->getDayOfMonth() == $other->getDayOfMonth() ) )
	{
	    return true ;
	}
	return false ;
    } // end function equals

    // get a new DateObj instance containg the date of the day following the date in this one
    function getNextDay()
    {
	global $oneDay ;
	$nextDay = new DateObj() ;
	$nextDayTimestamp = $this->timestamp + $oneDay ;
	$nextDay->setTimestamp( $nextDayTimestamp ) ;
	return $nextDay ;
    } // end function getNextDay

    function setToPrevDay()
    {
	global $oneDay ;
	$nextDay = new DateObj() ;
	$nextDayTimestamp = $this->timestamp - $oneDay ;
	$this->setTimestamp( $nextDayTimestamp ) ;
    } // end function getNextDay

    // get the date a month from the one in this object
    // if the current day of month is more than the number of days
    // in the next month, it returns the last day of the next month
    function getNextMonth()
    {
	$nextMonth = new DateObj() ;
	if ( $this->getMonth() == 12 )
	{
	    $nextMonth->setDate(
		$this->getYear()+1,
		1,
		$this->getDayOfMonth() ) ;
	}
	else
	{
	    $year = $this->getYear() ;
	    $month = $this->getMonth() + 1 ;
	    $day = $this->getDayOfMonth() ;
	    while ( ! checkdate( $month, $day, $year ) )
	    {
		$day-- ;
	    }
	    $nextMonth->setDate( $year, $month, $day ) ;
	}
	return $nextMonth ;
    } // end function getNextMonth

    // return the date one year after the date contained in this instance
    function getNextYear()
    {
	$newDate = new DateObj() ;
	if ( ( $this->getMonth() == 2 ) && ( $this->getDayOfMonth() == 29 ) )
	{
	    // this is a leap year, and we're on February
	    $newDate->setDate(
		$this->getYear()+1,
		3,
		1 ) ;
	}
	else
	{
	    $newDate->setDate(
		$this->getYear()+1,
		$this->getMonth(),
		$this->getDayOfMonth() ) ;
	}
	return $newDate ;
    } // end function getNextYear

    // print HTML remark with a debug message with contents of object
    function echoData()
    {
	echo "
	<!-- " .
	"timestamp:  " . $this->timestamp . ", " .
	"year:  " . $this->getYear() . ", " .
	"month: " . $this->getMonth() . ", " .
	"day:   " . $this->getDayOfMonth() .
	" -->" ;
    } // end function echoData

    // returns a string representing the date in the instance,
    // as created by the date() function using the given format
    function toString( $format = "d/m/Y" )
    {
	return date( $format, $this->timestamp ) ;
    } // end function toString

    // returns the difference between the dates in this instance and $other
    // valid units are: "year", "day", "hour", "minute", "seconds"
    // if $other is later, the result will be negative
    function diff( $other, $unit = "day" )
    {
	global $oneMinute, $oneHour, $oneDay, $oneYear ;
	if ( $unit == "minute" )
	{
	    return ( $this->timestamp - $other->timestamp ) / $oneMinute ;
	}
	if ( $unit == "hour" )
	{
	    return ( $this->timestamp - $other->timestamp ) / $oneHour ;
	}
	if ( $unit == "day" )
	{
	    return ( $this->timestamp - $other->timestamp ) / $oneDay ;
	}
	if ( $unit == "year" )
	{
	    return ( $this->timestamp - $other->timestamp ) / $oneYear ;
	}
    } // end function diff
    
    function setToEndOfPervMonth()
    {
	// for the end date, get the last day of the previous month
	$temp = new DateObj() ;
	$this->setDate( $this->getYear(), $this->getMonth(), 1 ) ;
	$this->setToPrevDay() ;
    }
} // end of class Date


// end of file dateUtils.php
?>