Date & Time
|
|
|
|
<?php /******************************************************************************
*******************************************************************************
DATETIME
Implements a date/time object and allow date and time manipulation.
Supports normal and fiscal dates
Can determine differences in days and seconds
Version
2006-07-08 1.0 Creation
*******************************************************************************
******************************************************************************/
/**
* datetime
*
* @package base
* @author Paul Kemper (pkemper@novell.com)
* @copyright None
*/
class datetime {
// {{{ constants
// }}} constants
// {{{ properties
/**
* year
*
* The full year (-9999..+9999)
*
* @access private
*/
private $year = 0;
/**
* month
*
* The month number (1..12)
*
* @access private
*/
private $month = 0;
/**
* day
*
* The day number (1..31)
*
* @access private
*/
private $day = 0;
/**
* hour
*
* The hours (0..23)
*
* @access private
*/
private $hour = -1;
/**
* minute
*
* The minutes (0..59)
*
* @access private
*/
private $minute = -1;
/**
* second
*
* The seconds (0..59)
*
* @access private
*/
private $second = -1;
/**
* microsecond
*
* The microseconds (0..999999)
*
* @access private
*/
private $microsecond = -1;
/**
* fiscalStart
*
* The month number of the start of the fiscal year (1..12)
*
* @access private
*/
private $fiscalStart = 11;
// }}} Properties
// {{{ __construct
/**
* Constructor
*
* Creates the date/time object with a default current date and time
*
* String formats supported for first parameter:
* YYYY-MM-DD[ hh[:mm[:ss[.uuuuuu]]]]
* M[M]/D[D]/YY[YY]
* D[D]-M[M]-YY[YY]
* D[D].M[M].YY[YY]
* D[D]:M[M]:YY[YY]
* D[D];M[M];YY[YY]
*
* @access public
* @param mixed $year Optional year number if numeric or date and time if string or datetime object
* @param integer $month Optional month number
* @param integer $day Optional day number
* @param integer $hour Optional hours
* @param integer $minute Optional minutes
* @param integer $seconds Optional seconds
* @param integer $microsec Optional microseconds
*/
public function __construct( $year = false, $month = false, $day = false,
$hour = false, $minute = false, $second = false, $microsecond = false )
{
if ( $year !== false && is_object( $year ) && is_a( $year, 'datetime' ) )
{
// A datetime object was passed. Copy its property values
$this->year = $year->getYear();
$this->month = $year->getMonth();
$this->day = $year->getDay();
$this->hour = $year->getHour();
$this->minute = $year->getMinute();
$this->second = $year->getSecond();
$this->microsecond = $year->getMicrosecond();
$this->fiscalStart = $year->getFiscalStart();
}
elseif ( $year !== false && is_numeric( $year ) )
{
// A numeric first parameter. Use as year
// Default parameters and illegal values will substitute the current date and time
$this->year = (int)(($year===false || $year < 9999 || $year > 9999)?date('Y'):$year);
$this->month = (int)(($month===false || $month < 1 || $month > 12)?date('m'):$month);
$this->day = (int)(($day===false || $day < 1 || $day > 31 )?date('d'):$day);
$this->hour = (int)(($hour===false || $hour < 0 || $hour > 23)?date('H'):$hour);
$this->minute = (int)(($minute===false || $minute < 0 || $minute > 59)?date('i'):$minute);
$this->second = (int)(($second===false || $second < 0 || $second > 59)?date('s'):$second);
if ( $microsecond===false || $microsecond < 0 || $microsecond > 999999 )
{
// Illegal microseconds
list( $this->microsecond, $sec ) = explode( ' ', microtime() );
$this->microsecond = round( $this->microsecond * 1000000.0 );
}
else
{
// Legal microseconds
$this->microsecond = $microsecond;
}
}
else
{
$this->parse( $year );
}
}
// }}} __construct
// {{{ getYear
/**
* getYear
*
* Return the year
*
* @access public
*/
public function getYear()
{
return $this->year;
}
// }}} getYear
// {{{ setYear
/**
* setYear
*
* Set the year if it is in range.
*
* @access public
* @param integer $year Year (does not convert 2-digit year to 19xx/20xx)
*/
public function setYear( $year )
{
if ( $year >= -9999 && $year <= 9999 )
{
$this->year = (int)$year;
}
}
// }}} setYear
// {{{ getQuarter
/**
* getQuarter
*
* Return trhe calendar quarter (1..4)
*
* @access public
* @return integer Calendar quarter
*/
public function getQuarter()
{
return floor(($this->month-1)/3+0.1666667) + 1;
}
// }}} getQuarter
// {{{ getMonth
/**
* getMonth
*
* Return the month
*
* @access public
*/
public function getMonth()
{
return $this->month;
}
// }}} getMonth
// {{{ setMonth
/**
* setMonth
*
* Set the month if it is in range.
*
* @access public
* @param integer $month Month number (1..12)
*/
public function setMonth( $month )
{
if ( $month >= 1 && $month <= 12 )
{
$this->month = (int)$month;
}
}
// }}} setMonth
// {{{ getDay
/**
* getDay
*
* Return the day
*
* @access public
*/
public function getDay()
{
return $this->day;
}
// }}} getDay
// {{{ setDay
/**
* setDay
*
* Set the day if it is in range.
*
* @access public
* @param integer $day Day number (1..31)
*/
public function setDay( $day )
{
if ( $day >= 1 && $day <= 31 )
{
$this->day = (int)$day;
}
}
// }}} setDay
// {{{ getHour
/**
* getHour
*
* Return the hour
*
* @access public
*/
public function getHour()
{
return $this->hour;
}
// }}} getHour
// {{{ setHour
/**
* setHour
*
* Set the hour if it is in range.
*
* @access public
* @param integer $hour Hour number (0..23)
*/
public function setHour( $hour )
{
if ( $hour >= 0 && $hour <= 23 )
{
$this->hour = (int)$hour;
}
}
// }}} setHour
// {{{ getMinute
/**
* getMinute
*
* Return the minutes
*
* @access public
*/
public function getMinute()
{
return $this->minute;
}
// }}} getMinute
// {{{ setMinute
/**
* setMinute
*
* Set the minutes if it is in range.
*
* @access public
* @param integer $minute Minute number (0..59)
*/
public function setMinute( $minute )
{
if ( $minute >= 0 && $minute <= 59 )
{
$this->minute = (int)$minute;
}
}
// }}} setMinute
// {{{ getSecond
/**
* getSecond
*
* Return the seconds
*
* @access public
*/
public function getSecond()
{
return $this->second;
}
// }}} getSecond
// {{{ setSecond
/**
* setSecond
*
* Set the seconds if it is in range.
*
* @access public
* @param integer $second Seconds number (0..59)
*/
public function setSecond( $second )
{
if ( $second >= 0 && $second <= 59 )
{
$this->second = (int)$second;
}
}
// }}} setSecond
// {{{ getMicrosecond
/**
* getMicrosecond
*
* Return the microseconds
*
* @access public
*/
public function getMicrosecond()
{
return $this->microsecond;
}
// }}} getMicrosecond
// {{{ setMicrosecond
/**
* setMicrosecond
*
* Set the microseconds if it is in range.
*
* @access public
* @param integer $msec Microsecond number (0..999999)
*/
public function setMicrosecond( $msec )
{
if ( $minute >= 0 && $msec <= 999999 )
{
$this->microsecond = (int)$msec;
}
}
// }}} setMicrosecond
// {{{ getFiscalStart
/**
* getFiscalStart
*
* Return the fiscal year starting month
*
* @access public
*/
public function getFiscalStart()
{
return $this->fiscalStart;
}
// }}} getFiscalStart
// {{{ setFiscalStart
/**
* setFiscalStart
*
* Set the fiscal year starting month if it is in range.
*
* @access public
* @param integer $start Fiscal year starting month (1..12)
*/
public function setFiscalStart( $start )
{
if ( $start >= 1 && $start <= 12 )
{
$this->fiscalStart = (int)$start;
}
}
// }}} setFiscalStart
// {{{ getTimestamp
/**
* getTimestamp
*
* Return the current date/time as Unix timestamp
*
* @access public
* @return integer Unix timestamp
*/
public function getTimestamp()
{
return date( 'U', mktime( $this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year ) );
}
// }}} getTimeStamp
// {{{ getEuroDate
/**
* getEuroDate
*
* Return the date in DD-MM-YYYY format
*
* @access public
* @return string Date in DD-MM-YYYY format
*/
public function getEuroDate()
{
return substr('0'.$this->day,-2) . '-' . substr('0'.$this->month,-2) . '-' . substr('000'.$this->year,-4);
}
// }}} getEuroDate
// {{{ getEuroDateTime
/**
* getEuroDateTime
*
* Return the date and time in DD-MM-YYYY hh:mm:ss.uuuuuu format
*
* @access public
* @return string Date in DD-MM-YYYY hh:mm:ss.uuuuuu format
*/
public function getEuroDateTime()
{
return substr('0'.$this->day,-2) . '-' . substr('0'.$this->month,-2) . '-' . substr('000'.$this->year,-4) . ' ' .
substr('0'.$this->hour,-2) . ':' . substr('0'.$this->minute,-2) . ':' . substr('0'.$this->second,-2);
}
// }}} getEuroDate
// {{{ getMySQLDate
/**
* getMySQLDate
*
* Return the date in YYYY-MM-DD format
*
* @access public
* @return string Date in YYYY-MM-DD format
*/
public function getMySQLDate()
{
return substr('000'.$this->year,-4) . '-' . substr('0'.$this->month,-2) . '-' . substr('0'.$this->day,-2);
}
// }}} getMySQLDate
// {{{ getMySQLDateTime
/**
* getMySQLDateTime
*
* Return the date and time in YYYY-MM-DD hh:mm:ss format
*
* @access public
* @return string Date in YYYY-MM-DD hh:mm:ss format
*/
public function getMySQLDateTime()
{
return substr('000'.$this->year,-4) . '-' . substr('0'.$this->month,-2) . '-' . substr('0'.$this->day,-2) . ' ' .
substr('0'.$this->hour,-2) . ':' . substr('0'.$this->minute,-2) . ':' . substr('0'.$this->second,-2);
}
// }}} getMySQLDateTime
// {{{ getUSDate
/**
* getUSDate
*
* Return the date in MM/DD/YYYY format
*
* @access public
* @return string Date in MM/DD/YYYY format
*/
public function getUSDate()
{
return $this->month . '/' . $this->day . '/' . substr('000'.$this->year,-4);
}
// }}} getUSDate
// {{{ getUSDateTime
/**
* getUSDateTime
*
* Return the date and time in MM/DD/YYYY h:mm:ssa format
*
* @access public
* @return string Date in MM/DD/YYYY h:mm:ssa format
*/
public function getUSDateTime()
{
return $this->month . '/' . $this->day . '/' . substr('000'.$this->year,-4) . ' ' .
((($this->hour-1) % 12) + 1) . ':' . substr('0'.$this->minute,-2) . ':' . substr('0'.$this->second,-2) . (($this->hour>=12)?'pm':'am');
}
// }}} getUSDateTime
// {{{ getJulianDate
/**
* getJulianDate
*
* Return the Julian date portion
*
* @access public
* @return integer Julian date
*/
public function getJulianDate()
{
return gregoriantojd( $this->month, $this->day, $this->year );
}
// }}} getJulianDate
// {{{ setJulianDate
/**
* setJulianDate
*
* Set the Julian date
*
* @access public
* @param integer $jdt Julian date number
*/
public function setJulianDate( $jdt )
{
list( $this->month, $this->day, $this->year ) = explode('/',jdtogregorian($jdt));
}
// }}} setJulianDate
// {{{ getJulianDateTime
/**
* getJulianDateTime
*
* Return the Julian date and time (Julian day starts at 12 noon)
*
* @access public
* @return float Julian date and time
*/
public function getJulianDateTime()
{
// Get the Julian date portion
$day = gregoriantojd( $this->month, $this->day, $this->year );
// Convert the time portion to day
$time = ($this->hour * 3600.0 + $this->minute * 60.0 + $this->second + $this->microsecond / 1000000.0 )/(24.0 * 60.0 * 60.0) - 0.5;
return $day + $time;
}
// }}} getJulianDateTime
// {{{ setJulianDateTime
/**
* setJulianDateTime
*
* Set the Julian date and time
*
* @access public
* @param float $jdt Julian date and time
*/
public function setJulianDateTime( $jdt )
{
if ( $jdt < 0.5 )
{
$this->hour = 12;
}
else
{
$this->hour = 0;
}
// Correct for the fact that Julian days start at noon
$jdt += 0.5;
$this->setJulianDate( floor( $jdt ) );
$jdt -= 0.5;
$jdt -= floor( $jdt );
if ( $jdt > 0.5 )
{
$ldt -= 0.5;
}
$jdt *= 24;
$this->hour += floor( $jdt );
$jdt -= floor( $jdt );
$jdt *= 60;
$this->minute = floor( $jdt );
$jdt -= floor( $jdt );
$jdt *= 60;
$this->second = floor( $jdt );
$jdt -= floor( $jdt );
$jdt *= 1000000;
$this->microsecond = floor( $jdt );
}
// }}} setJulianDateTime
// {{{ addYear
/**
* addYear
*
* Add a number of years
*
* @access public
* @param integer $years Years to add (positive or negative)
*/
public function addYear( $year )
{
$this->year += $year;
}
// }}} addYear
// {{{ addMonth
/**
* Add a number of months
*
* @access public
* @param integer $month Number of month to add (positive or negative)
*/
public function addMonth( $month )
{
$this->month += $month;
// Normalize months that are too big
while ( $this->month > 12 )
{
$this->year++;
$this->month -= 12;
}
// Normalize months that are too small
while ( $this->month < 1 )
{
$this->year--;
$this->month += 12;
}
}
// }}} addMonth
// {{{ addDay
/**
* addDay
*
* Add days to the current date
*
* @access public
* @param integer $day Days to add (positive or negative)
*/
public function addDay( $day )
{
list( $this->month, $this->day, $this->year ) = explode('/',jdtogregorian(gregoriantojd( $this->month, $this->day, $this->year ) + $day));
}
// }}} addDay
// {{{ addHour
/**
* addHour
*
* Add hours to the current date and time
*
* @access public
* @param integer $hour Hours to add (positive or negative)
*/
public function addHour( $hour )
{
// Add the hours
$this->hour += $hour;
// Normalize the hour if it is too big (and adjust the date)
while ( $this->hour > 23 )
{
$this->addDay(1);
$this->hour -= 24;
}
// Normalize the hour if it is too small (and adjust the date)
while ( $this->hour < 0 )
{
$this->addDay(-1);
$this->hour += 24;
}
}
// }}} addHour
// {{{ addMinute
/**
* addMinute
*
* Add minutes to the current date and time
*
* @access public
* @param integer $min Minutes to add (positive or negative)
*/
public function addMinute( $min )
{
// Add the minutes
$this->minute += $min;
// Normalize minutes which are too big
while ( $this->minute > 59 )
{
$this->addHour(1);
$this->minute -= 60;
}
// Normalize minutes which are too small
while ( $this->minute < 0 )
{
$this->addHour(-1);
$this->minute += 60;
}
}
// }}} addMinute
// {{{ addSecond
/**
* addSecond
*
* Add seconds to the current time
*
* @access public
* @param integer $sec Seconss to add (positive or negative)
*/
public function addSecond( $sec )
{
// Add the seconds
$this->second += $sec;
// Normalize seconds which are too big
while ( $this->second > 59 )
{
$this->addMinute(1);
$this->second -= 60;
}
// Normalize seconds which are too small
while ( $this->second < 0 )
{
$this->addMinute(-1);
$this->second += 60;
}
}
// }}} addSecond
// {{{ addMicrosecond
/**
* addMicrosecond
*
* Add microseconds to the current date and time
*
* @access public
* @param integer $msec Microseconds to add (positive or negative)
*/
public function addMicrosecond( $msec )
{
// Add the microseconds
$this->microsecond += $msec;
// Normalize microseconds which are too big
while ( $this->microsecond > 999999 )
{
$this->addSecond(1);
$this->microsecond -= 1000000;
}
// Normalize microseconds which are too small
while ( $this->microsecond < 0 )
{
$this->addSecond(-1);
$this->microsecond += 1000000;
}
}
// }}} addMicrosecond
// {{{ addDateTime
/**
* addDateTime
*
* Add a date/time to the current date/time
*
* @access public
* @param object $dt Reference to datetime object to add
*/
public function addDateTime( &$dt )
{
$this->addMicrosecond( $dt->getMicrosecond() );
$this->addSecond( $dt->getSecond() );
$this->addMinute( $dt->getMinute() );
$this->addHour( $dt->getHour() );
$this->addDay( $dt->getDay() );
$this->addMonth( $dt->getMonth() );
$this->addYear( $dt->getYear() );
}
// }}} addDateTime
// {{{ diffDays
/**
* diffDays
*
* Return the difference between date in number of days
* Result is this date minus passed date
*
* @access public
* @param datetime $date Datetime object to subtract from this date
* @return integer Difference in whole days
*/
public function diffDays( &$date )
{
return $this->getJulianDate() - $date->getJulianDate();
}
// }}} diffDays
// {{{ diffSeconds
/**
* diffSeconds
*
* Return difference between date/time in seconds
* Result is this date/time minus passed date/time
*
* @access public
* @param datetime $date Datetime object to subtract from this date/time
* @return integer Difference in whole seconds
*/
public function diffSeconds( &$date )
{
// Day difference in seconds
return ($this->getJulianDate()-$date->getJulianDate()) * 24 * 60 * 60 +
($this->hour - $date->getHour()) * 60 * 60 +
($this->minute - $date->getMinute()) * 60 +
($this->second - $date->getSecond());
}
// }}} diffSeconds
// {{{ diffMicroseconds
/**
* diffMicroseconds
*
* Return different between date.time in microseconds
*
* @access public
* @param datetime $date Datetime object to subtract from this date/time
* @return integer Difference in whole microseconds
*/
public function diffMicroseconds( &$date )
{
// Day difference in seconds
return ($this->getJulianDate() - $date->getJulianDate()) * 24 * 60 * 60 * 1000000 +
($this->hour - $date->getHour()) * 60 * 60 * 1000000 +
($this->minute - $date->getMinute()) * 60 * 1000000 +
($this->second - $date->getSecond()) * 1000000 +
($this->microsecond - $date->getMicrosecond());
}
// }}} diffMicroseconds
// {{{ setQuarterStart
/**
* setQuarterStart
*
* Set the date to the start of the calendar quarter
*
* @access public
*/
public function setQuarterStart()
{
// Adjust the month
$this->month = floor(($this->month-1)/3+0.1666667)*3+1;
// Set day to 1
$this->day = 1;
}
// }}} setQuarterStart
// {{{ setQuarterEnd
/**
* setQuarterEnd
*
* Set date to the last day in the calendar quarter
*
* @access public
*/
public function setQuarterEnd()
{
// Push date forward into the next quarter
$this->addMonth( 3 );
// Set date to first day of next quarter
$this->setQuarterStart();
// Move date back by 1 day
$this->addDay(-1);
}
// }}} setQuarterEnd
// {{{ setFiscalQuarterStart
/**
* setFiscalQuarterStart
*
* Set date to the frist day in the fiscal quarter
*
* @access public
*/
public function setFiscalQuarterStart()
{
// Adjust the month
$this->month = floor(($this->month-$this->fiscalStart)/3+0.1666667)*3+$this->fiscalStart;
// Set day to 1
$this->day = 1;
}
// }}} setFiscalQuarterStart
// {{{ setFiscalQuarterEnd
/**
* setFiscalQuarterEnd
*
* Set date to the last day of the fiscal quarter
*
* @access public
*/
public function setFiscalQuarterEnd()
{
// Push date forward into next quarter
$this->addMonth(3);
// Set date to fist day of next quarter
$this->setFiscalQuarterStart();
// Move date back by 1 day
$this->addDay(-1);
}
// }}} setFiscalQuarterEnd
// {{{ getDayOfQuarter
/**
* getDayOfQuarter
*
* Return the day number in the calendar quarter (first day is 0)
*
* @access public
* @return integer Days into the calendar quarter
*/
public function getDayOfQuarter()
{
// Take Julian date of this date
$jd1 = $this->getJulianDate();
// Create a copy of the date
$dt0 = new datetime( $this );
// Set copy to first day of calendar quarter
$dt0->setQuarterStart();
// Take Julian date of first day of calendar quarter
$jd0 = $dt0->getJulianDate();
// Return the difference
return $jd1 - $jd0;
}
// }}} getDayOfQuarter
// {{{ getDayOfFiscalQuarter
/**
* getDayOfFiscalQuarter
*
* Return the day number in the fiscal quarter (first day is 0)
*
* @access public
* @return integer Days into the fiscal quarter
*/
public function getDayOfFiscalQuarter()
{
// Take Julian date of this date
$jd1 = $this->getJulianDate();
// Create a copy of the date
$dt0 = new datetime( $this );
// Set copy to first day of fiscal quarter
$dt0->setFiscalQuarterStart();
// Take Julian date of first day of fiscal quarter
$jd0 = $dt0->getJulianDate();
// Return the difference
return $jd1 - $jd0;
}
// {{{ getDayOfFiscalQuarter
// {{{ getDayOfYear
/**
* getDayOfYear
*
* Return the day into the calendar year (1 January is 0)
*
* @access public
* @return integer Days into the calendar year
*/
public function getDayOfYear()
{
// Take Julian date of this date
$jd1 = $this->getJulianDate();
// Create a copy of the date
$dt0 = new datetime( $this );
// Set copy to first day of calendar year
$dt0->setMonth( 1 );
$dt0->setDay( 1 );
// Take Julian date of first day of calendar year
$jd0 = $dt0->getJulianDate();
// Return the difference
return $jd1 - $jd0;
}
// }}} getDayOfYear
// {{{ getDayOfFiscalYear
/**
* getDayOfFiscalYear
*
* Return the day into the fiscal year (1st of fiscal start month is 0)
*
* @access public
* @return integer Days into the fiscal year
*/
public function getDayOfFiscalYear()
{
// Take Julian date of this date
$jd1 = $this->getJulianDate();
// Create copy of this date
$dt0 = new datetime( $this );
// Set month ro fiscal starting month
$dt0->setMonth( $this->fiscalStart );
// Check if current date if before starting month. If so, asjust the year
if ( $this->getMonth() < $this->fiscalStart )
{
$dt0->addYear( -1 );
}
// Set copied date to first of month
$dt0->setDay( 1 );
// Get Julian date of first day of fiscal year
$jd0 = $dt0->getJulianDate();
// Return the difference
return $jd1 - $jd0;
}
// }}} getDayOfFiscalYear
// {{{ getFiscalYear
/**
* getFiscalYear
*
* Return the fiscal year of the current date
*
* @access public
* @return integer Fiscal year
*/
public function getFiscalYear()
{
// Check if the current month is past the fiscal starting month.
if ( $this->month >= $this->fiscalStart )
{
// Yes, passed the fiscal starting month. Return the next year if month > 6
if ( $this->month > 6 )
{
return $this->year + 1;
}
else
{
return $this->year;
}
}
else
{
// Current month not past fiscal starting month. Return current year
return $this->year;
}
}
// }}} getFiscalYear
// {{{ getFiscalQuarter
/**
* getFiscalQuarter
*
* Return the fiscal quarter of the current date (1..4)
*
* @access public
* @return integer Fiscal quarter of current date
*/
public function getFiscalQuarter()
{
return (floor(($this->month - $this->fiscalStart)/3+0.1666667) + 4) % 4 + 1;
}
// }}} getFiscalQuarter
// {{{ getFiscalMonth
/**
* getFiscalMonth
*
* Return the fiscal month
*
* @access public
* @return integer Fiscal month (1..12)
*/
public function getFiscalMonth()
{
$m = $this->month - $this->fiscalStart + 1;
if ( $m < 1 )
{
$m += 12;
}
return $m;
}
// }}} getFiscalMonth
// {{{ getDOW
/**
* getDOW
*
* Return day of the week (0=sunday)
*
* @access public
* @return integer Day of the week (0=sunday)
*/
public function getDOW()
{
return (int)$this->date( 'w' );
}
// }}} getDOW
// {{{ parse
/**
* parse
*
* Parse a date/time string into the date/time properties
*
* @access public
* @param string $date String date to be parsed
*/
public function parse( $date )
{
// A string first parameter. Use as date/time string
$matches = array();
// Test for YYYY-MM-DD[ hh[:mm[:ss[.uuuuuu]]]]
if ( preg_match( '/(dddd)-(dd)-(dd)(s(dd)(:(dd)(:(dd)(.(d)+){0,1}){0,1}){0,1}){0,1}/i', $date, $matches ) )
{
$this->year = ($matches[1] < -9999 || $matches[1] > 9999)?date('Y'):$matches[1];
$this->month = ($matches[2] < 1 || $matches[2] > 12)?date('m'):$matches[2];
$this->day = ($matches[3] < 1 || $matches[3] > 31)?date('d'):$matches[3];
$this->hour = (isset($matches[5]) && $matches[5] >= 0 && $matches[5] < 24)?$matches[5]:date('H');
$this->minute = (isset($matches[7]) && $matches[7] >= 0 && $matches[7] < 60)?$matches[7]:date('i');
$this->second = (isset($matches[9]) && $matches[9] >= 0 && $matches[9] < 60)?$matches[9]:date('s');
if ( !isset($matches[10]) || $matches[10] < 0 || $matches[10] > 999999 )
{
// Illegal microseconds
list( $this->microsecond, $sec ) = explode( ' ', microtime() );
$this->microsecond = round( $this->microsecond * 1000000.0 );
}
else
{
// Legal microseconds
$this->microsecond = substr( $matches[10], 1 );
}
}
// Test for M[M]/D[D]/YY[YY]
elseif ( preg_match( '/((d){1,2}){1}/((d){1,2}){1}/((dd){1,2}){1}/i', $date, $matches ) )
{
$this->year = ($matches[5] < -9999 || $matches[5] > 9999)?date('Y'):$matches[5];
if ( $this->year < 100 )
{
$this->year += ($this->year<30)?2000:1900;
}
$this->month = ($matches[1] < 1 || $matches[1] > 12)?date('m'):$matches[1];
$this->day = ($matches[3] < 1 || $matches[3] > 31)?date('d'):$matches[3];
$this->hour = date('H');
$this->minute = date('i');
$this->second = isset($matches[6])?$matches[6]:date('s');
list( $this->microsecond, $sec ) = explode( ' ', microtime() );
$this->microsecond = round( $this->microsecond * 1000000.0 );
}
// Test for D[D]-M[M]-YY[YY] or D[D].M[M].YY[YY] or D[D]:M[M]:YY[YY] or D[D];M[M];YY[YY]
elseif ( preg_match( '/((d){1,2}){1}[-:;.]((d){1,2}){1}[-:;.]((dd){1,2}){1}/i', $date, $matches ) )
{
$this->year = ($matches[5] < -9999 || $matches[5] > 9999)?date('Y'):$matches[5];
if ( $this->year < 100 )
{
$this->year += ($this->year<30)?2000:1900;
}
$this->month = ($matches[3] < 1 || $matches[3] > 12)?date('m'):$matches[3];
$this->day = ($matches[1] < 1 || $matches[1] > 31)?date('d'):$matches[1];
$this->hour = date('H');
$this->minute = date('i');
$this->second = date('s');
list( $this->microsecond, $sec ) = explode( ' ', microtime() );
$this->microsecond = round( $this->microsecond * 1000000.0 );
}
else
{
// No pattern match. Set the default date/time to now
$this->year = date('Y');
$this->month = date('m');
$this->day = date('d');
$this->hour = date('H');
$this->minute = date('i');
$this->second = date('s');
list( $this->microsecond, $sec ) = explode( ' ', microtime() );
$this->microsecond = round( $this->microsecond * 1000000.0 );
}
// Convert all elements to integer
$this->year = (int)$this->year;
$this->month = (int)$this->month;
$this->day = (int)$this->day;
$this->hour = (int)$this->hour;
$this->minute = (int)$this->minute;
$this->second = (int)$this->second;
$this->microsecond = (int)$this->microsecond;
}
// }}} parse
// {{{ date
/**
* date
*
* Return a string representation of the date, using the same paramaters as the date() function
*
* @access public
* @param string $format date() format specifiers
* @return string Formatted date
*/
public function date( $format )
{
return date( $format, mktime( $this->hour, $this->minute, $this->second, $this->month, $this->day, $this->year ) );
}
// }}} date } ?>
|
|
|
Usage Example
|
<?php
$dtEnd = new datetime( $_POST['enddate'] ); // Accept whatever the user typed (as long as it is not text)
// Make a working copy $dt = new datetime( $dtEnd );
// Find the first day in this fiscal quarter (fiscal years starts in October/default is November)
$dt->setFirscalStart( 10 ); $dt->setFiscalQuarterStart(); // Print some bogus result echo 'Hi, '.$dtEnd->getEuroDate().' is '.$dtEnd->diffDays( $dt ).' days into the current fiscal quarter.'; ?>
|
|
|
Rate This Script
|
|
|
|