<?php
/**
* @version $Id: date.php 20196 2011-01-09 02:40:25Z ian $
* @package Joomla.Framework
* @subpackage Utilities
* @copyright Copyright (C) 2005 - 2011 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
// No direct access
defined('JPATH_BASE') or die;
/**
* JDate is a class that stores a date and provides logic to manipulate
* and render that date in a variety of formats.
*
* @package Joomla.Framework
* @subpackage Utilities
* @since 1.5
*/
class JDate extends DateTime
{
const DAY_ABBR = "\x021\x03";
const DAY_NAME = "\x022\x03";
const MONTH_ABBR = "\x023\x03";
const MONTH_NAME = "\x024\x03";
/**
* The format string to be applied when using the __toString() magic method.
*
* @var string
* @since 1.6
*/
public static $format = 'Y-m-d H:i:s';
/**
* Placeholder for a DateTimeZone object with GMT as the time zone.
*
* @var object
* @since 1.6
*/
protected static $gmt;
/**
* Placeholder for a DateTimeZone object with the default server
* time zone as the time zone.
*
* @var object
* @since 1.6
*/
protected static $stz;
/**
* An array of offsets and time zone strings representing the available
* options from Joomla! 1.5 and below.
*
* @deprecated Deprecated since 1.6
*
* @var array
* @since 1.6
*/
protected static $offsets = array(
'-12' => 'Etc/GMT-12',
'-11' => 'Pacific/Midway',
'-10' => 'Pacific/Honolulu',
'-9.5' => 'Pacific/Marquesas',
'-9' => 'US/Alaska',
'-8' => 'US/Pacific',
'-7' => 'US/Mountain',
'-6' => 'US/Central',
'-5' => 'US/Eastern',
'-4.5' => 'America/Caracas',
'-4' => 'America/Barbados',
'-3.5' => 'Canada/Newfoundland',
'-3' => 'America/Buenos_Aires',
'-2' => 'Atlantic/South_Georgia',
'-1' => 'Atlantic/Azores',
'0' => 'Europe/London',
'1' => 'Europe/Amsterdam',
'2' => 'Europe/Istanbul',
'3' => 'Asia/Riyadh',
'3.5' => 'Asia/Tehran',
'4' => 'Asia/Muscat',
'4.5' => 'Asia/Kabul',
'5' => 'Asia/Karachi',
'5.5' => 'Asia/Calcutta',
'5.75' => 'Asia/Katmandu',
'6' => 'Asia/Dhaka',
'6.5' => 'Indian/Cocos',
'7' => 'Asia/Bangkok',
'8' => 'Australia/Perth',
'8.75' => 'Australia/West',
'9' => 'Asia/Tokyo',
'9.5' => 'Australia/Adelaide',
'10' => 'Australia/Brisbane',
'10.5' => 'Australia/Lord_Howe',
'11' => 'Pacific/Kosrae',
'11.5' => 'Pacific/Norfolk',
'12' => 'Pacific/Auckland',
'12.75' => 'Pacific/Chatham',
'13' => 'Pacific/Tongatapu',
'14' => 'Pacific/Kiritimati'
);
/**
* The DateTimeZone object for usage in rending dates as strings.
*
* @var object
* @since 1.6
*/
protected $_tz;
/**
* Constructor.
*
* @param string String in a format accepted by strtotime(), defaults to "now".
* @param mixed Time zone to be used for the date.
* @return void
* @since 1.5
*
* @throws JException
*/
public function __construct($date = 'now', $tz = null)
{
// Create the base GMT and server time zone objects.
if (empty(self::$gmt) || empty(self::$stz)) {
self::$gmt = new DateTimeZone('GMT');
self::$stz = new DateTimeZone(@date_default_timezone_get());
}
// If the time zone object is not set, attempt to build it.
if (!($tz instanceof DateTimeZone)) {
if ($tz === null) {
$tz = self::$gmt;
} elseif (is_numeric($tz)) {
// Translate from offset.
$tz = new DateTimeZone(self::$offsets[(string) $tz]);
}
elseif (is_string($tz)) {
$tz = new DateTimeZone($tz);
}
}
// If the date is numeric assume a unix timestamp and convert it.
date_default_timezone_set('UTC');
$date = is_numeric($date)? date('c', $date) : $date;
// Call the DateTime constructor.
parent::__construct($date, $tz);
// Set the timezone object for access later.
$this->_tz = $tz;
}
/**
* Magic method to access properties of the date given by class to the format method.
*
* @param string The name of the property.
* @return mixed A value if the property name is valid, null otherwise.
* @since 1.6
*/
public function __get($name)
{
$value = null;
switch ($name) {
case 'daysinmonth':
$value = $this->format('t', true);
break;
case 'dayofweek':
$value = $this->format('N', true);
break;
case 'dayofyear':
$value = $this->format('z', true);
break;
case 'day':
$value = $this->format('d', true);
break;
case 'hour':
$value = $this->format('H', true);
break;
case 'isleapyear':
$value = (boolean) $this->format('L', true);
break;
case 'hour':
$value = $this->format('H', true);
break;
case 'minute':
$value = $this->format('i', true);
break;
case 'month':
$value = $this->format('m', true);
break;
case 'ordinal':
$value = $this->format('S', true);
break;
case 'second':
$value = $this->format('s', true);
break;
case 'week':
$value = $this->format('W', true);
break;
case 'year':
$value = $this->format('Y', true);
break;
default:
$trace = debug_backtrace();
trigger_error(
'Undefined property via __get(): ' . $name .
' in ' . $trace[0]['file'] .
' on line ' . $trace[0]['line'],
E_USER_NOTICE
);
}
return $value;
}
/**
* Magic method to render the date object in the format specified in the public
* static member JDate::$format.
*
* @return string The date as a formatted string.
* @since 1.6
*/
public function __toString()
{
return (string) parent::format(self::$format);
}
/**
* Translates day of week number to a string.
*
* @param integer The numeric day of the week.
* @param boolean Return the abreviated day string?
* @return string The day of the week.
* @since 1.5
*/
protected function dayToString($day, $abbr = false)
{
switch ($day) {
case 0: return $abbr ? JText::_('SUN') : JText::_('SUNDAY');
case 1: return $abbr ? JText::_('MON') : JText::_('MONDAY');
case 2: return $abbr ? JText::_('TUE') : JText::_('TUESDAY');
case 3: return $abbr ? JText::_('WED') : JText::_('WEDNESDAY');
case 4: return $abbr ? JText::_('THU') : JText::_('THURSDAY');
case 5: return $abbr ? JText::_('FRI') : JText::_('FRIDAY');
case 6: return $abbr ? JText::_('SAT') : JText::_('SATURDAY');
}
}
/**
* Gets the date as a formatted string in a local calendar.
*
* @param string The date format specification string (see {@link PHP_MANUAL