[ Index ]

PHP Cross Reference of Joomla 1.5.26 DE

title

Body

[close]

/libraries/joomla/utilities/ -> date.php (source)

   1  <?php
   2  /**
   3  * @version        $Id: date.php 14401 2010-01-26 14:10:00Z louis $
   4  * @package        Joomla.Framework
   5  * @subpackage    Utilities
   6  * @copyright    Copyright (C) 2005 - 2010 Open Source Matters. All rights reserved.
   7  * @license        GNU/GPL, see LICENSE.php
   8  * Joomla! is free software. This version may have been modified pursuant
   9  * to the GNU General Public License, and as distributed it includes or
  10  * is derivative of works licensed under the GNU General Public License or
  11  * other free or open source software licenses.
  12  * See COPYRIGHT.php for copyright notices and details.
  13  */
  14  
  15  // Check to ensure this file is within the rest of the framework
  16  defined('JPATH_BASE') or die();
  17  
  18  /**
  19   * JDate is a class that stores a date
  20   *
  21   * @package        Joomla.Framework
  22   * @subpackage    Utilities
  23   * @since        1.5
  24   */
  25  class JDate extends JObject
  26  {
  27      /**
  28       * Unix timestamp
  29       *
  30       * @var     int|boolean
  31       * @access  protected
  32       */
  33      var $_date = false;
  34  
  35      /**
  36       * Time offset (in seconds)
  37       *
  38       * @var     string
  39       * @access  protected
  40       */
  41      var $_offset = 0;
  42  
  43      /**
  44       * Creates a new instance of JDate representing a given date.
  45       *
  46       * Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
  47       * If not specified, the current date and time is used.
  48       *
  49       * @param mixed $date optional the date this JDate will represent.
  50       * @param int $tzOffset optional the timezone $date is from
  51       */
  52  	function __construct($date = 'now', $tzOffset = 0)
  53      {
  54          if ($date == 'now' || empty($date))
  55          {
  56              $this->_date = strtotime(gmdate("M d Y H:i:s", time()));
  57              return;
  58          }
  59  
  60          $tzOffset *= 3600;
  61          if (is_numeric($date))
  62          {
  63              $this->_date = $date - $tzOffset;
  64              return;
  65          }
  66  
  67          if (preg_match('~(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun),\\s+)?(\\d{1,2})\\s+([a-zA-Z]{3})\\s+(\\d{4})\\s+(\\d{2}):(\\d{2}):(\\d{2})\\s+(.*)~i',$date,$matches))
  68          {
  69              $months = Array(
  70                  'jan' => 1, 'feb' => 2, 'mar' => 3, 'apr' => 4,
  71                  'may' => 5, 'jun' => 6, 'jul' => 7, 'aug' => 8,
  72                  'sep' => 9, 'oct' => 10, 'nov' => 11, 'dec' => 12
  73              );
  74              $matches[2] = strtolower($matches[2]);
  75              if (! isset($months[$matches[2]])) {
  76                  return;
  77              }
  78              $this->_date = mktime(
  79                  $matches[4], $matches[5], $matches[6],
  80                  $months[$matches[2]], $matches[1], $matches[3]
  81              );
  82              if ($this->_date === false) {
  83                  return;
  84              }
  85  
  86              if ($matches[7][0] == '+') {
  87                  $tzOffset = 3600 * substr($matches[7], 1, 2)
  88                      + 60 * substr($matches[7], -2);
  89              } elseif ($matches[7][0] == '-') {
  90                  $tzOffset = -3600 * substr($matches[7], 1, 2)
  91                      - 60 * substr($matches[7], -2);
  92              } else {
  93                  if (strlen($matches[7]) == 1) {
  94                      $oneHour = 3600;
  95                      $ord = ord($matches[7]);
  96                      if ($ord < ord('M')) {
  97                          $tzOffset = (ord('A') - $ord - 1) * $oneHour;
  98                      } elseif ($ord >= ord('M') && $matches[7] != 'Z') {
  99                          $tzOffset = ($ord - ord('M')) * $oneHour;
 100                      } elseif ($matches[7] == 'Z') {
 101                          $tzOffset = 0;
 102                      }
 103                  }
 104                  switch ($matches[7]) {
 105                      case 'UT':
 106                      case 'GMT': $tzOffset = 0;
 107                  }
 108              }
 109              $this->_date -= $tzOffset;
 110              return;
 111          }
 112          if (preg_match('~(\\d{4})-(\\d{2})-(\\d{2})[T\s](\\d{2}):(\\d{2}):(\\d{2})(.*)~', $date, $matches))
 113          {
 114              $this->_date = mktime(
 115                  $matches[4], $matches[5], $matches[6],
 116                  $matches[2], $matches[3], $matches[1]
 117              );
 118              if ($this->_date == false) {
 119                  return;
 120              }
 121              if (isset($matches[7][0])) {
 122                  if ($matches[7][0] == '+' || $matches[7][0] == '-') {
 123                      $tzOffset = 60 * (
 124                          substr($matches[7], 0, 3) * 60 + substr($matches[7], -2)
 125                      );
 126                  } elseif ($matches[7] == 'Z') {
 127                      $tzOffset = 0;
 128                  }
 129              }
 130              $this->_date -= $tzOffset;
 131              return;
 132          }
 133          $this->_date = (strtotime($date) == -1) ? false : strtotime($date);
 134          if ($this->_date) {
 135              $this->_date -= $tzOffset;
 136          }
 137      }
 138  
 139      /**
 140       * Set the date offset (in hours)
 141       *
 142       * @access public
 143       * @param float The offset in hours
 144       */
 145  	function setOffset($offset) {
 146          $this->_offset = 3600 * $offset;
 147      }
 148  
 149      /**
 150       * Get the date offset (in hours)
 151       *
 152       * @access public
 153       * @return integer
 154       */
 155  	function getOffset() {
 156          return ((float) $this->_offset) / 3600.0;
 157      }
 158  
 159      /**
 160       * Gets the date as an RFC 822 date.
 161       *
 162       * @return a date in RFC 822 format
 163       * @link http://www.ietf.org/rfc/rfc2822.txt?number=2822 IETF RFC 2822
 164       * (replaces RFC 822)
 165       */
 166  	function toRFC822($local = false)
 167      {
 168          $date = ($local) ? $this->_date + $this->_offset : $this->_date;
 169          $date = ($this->_date !== false) ? date('D, d M Y H:i:s', $date).' +0000' : null;
 170          return $date;
 171      }
 172  
 173      /**
 174       * Gets the date as an ISO 8601 date.
 175       *
 176       * @return a date in ISO 8601 (RFC 3339) format
 177       * @link http://www.ietf.org/rfc/rfc3339.txt?number=3339 IETF RFC 3339
 178       */
 179  	function toISO8601($local = false)
 180      {
 181          $date   = ($local) ? $this->_date + $this->_offset : $this->_date;
 182          $offset = $this->getOffset();
 183          $offset = ($local && $this->_offset) ? sprintf("%+03d:%02d", $offset, abs(($offset-intval($offset))*60) ) : 'Z';
 184          $date   = ($this->_date !== false) ? date('Y-m-d\TH:i:s', $date).$offset : null;
 185          return $date;
 186      }
 187  
 188      /**
 189       * Gets the date as in MySQL datetime format
 190       *
 191       * @return a date in MySQL datetime format
 192       * @link http://dev.mysql.com/doc/refman/4.1/en/datetime.html MySQL DATETIME
 193       * format
 194       */
 195  	function toMySQL($local = false)
 196      {
 197          $date = ($local) ? $this->_date + $this->_offset : $this->_date;
 198          $date = ($this->_date !== false) ? date('Y-m-d H:i:s', $date) : null;
 199          return $date;
 200      }
 201  
 202      /**
 203       * Gets the date as UNIX time stamp.
 204       *
 205       * @return a date as a unix time stamp
 206       */
 207  	function toUnix($local = false)
 208      {
 209          $date = null;
 210          if ($this->_date !== false) {
 211              $date = ($local) ? $this->_date + $this->_offset : $this->_date;
 212          }
 213          return $date;
 214      }
 215  
 216      /**
 217       * Gets the date in a specific format
 218       *
 219       * Returns a string formatted according to the given format. Month and weekday names and
 220       * other language dependent strings respect the current locale
 221       *
 222       * @param string $format  The date format specification string (see {@link PHP_MANUAL#strftime})
 223       * @return a date in a specific format
 224       */
 225  	function toFormat($format = '%Y-%m-%d %H:%M:%S')
 226      {
 227          $date = ($this->_date !== false) ? $this->_strftime($format, $this->_date + $this->_offset) : null;
 228  
 229          return $date;
 230      }
 231  
 232      /**
 233       * Translates needed strings in for JDate::toFormat (see {@link PHP_MANUAL#strftime})
 234       *
 235       * @access protected
 236       * @param string $format The date format specification string (see {@link PHP_MANUAL#strftime})
 237       * @param int $time Unix timestamp
 238       * @return string a date in the specified format
 239       */
 240  	function _strftime($format, $time)
 241      {
 242          if(strpos($format, '%a') !== false)
 243              $format = str_replace('%a', $this->_dayToString(date('w', $time), true), $format);
 244          if(strpos($format, '%A') !== false)
 245              $format = str_replace('%A', $this->_dayToString(date('w', $time)), $format);
 246          if(strpos($format, '%b') !== false)
 247              $format = str_replace('%b', $this->_monthToString(date('n', $time), true), $format);
 248          if(strpos($format, '%B') !== false)
 249              $format = str_replace('%B', $this->_monthToString(date('n', $time)), $format);
 250          $date = strftime($format, $time);
 251          return $date;
 252      }
 253  
 254      /**
 255       * Translates month number to string
 256       *
 257       * @access protected
 258       * @param int $month The numeric month of the year
 259       * @param bool $abbr Return the abreviated month string?
 260       * @return string month string
 261       */
 262  	function _monthToString($month, $abbr = false)
 263      {
 264          switch ($month)
 265          {
 266              case 1:  return $abbr ? JText::_('JANUARY_SHORT')   : JText::_('JANUARY');
 267              case 2:  return $abbr ? JText::_('FEBRUARY_SHORT')  : JText::_('FEBRUARY');
 268              case 3:  return $abbr ? JText::_('MARCH_SHORT')     : JText::_('MARCH');
 269              case 4:  return $abbr ? JText::_('APRIL_SHORT')     : JText::_('APRIL');
 270              case 5:  return $abbr ? JText::_('MAY_SHORT')       : JText::_('MAY');
 271              case 6:  return $abbr ? JText::_('JUNE_SHORT')      : JText::_('JUNE');
 272              case 7:  return $abbr ? JText::_('JULY_SHORT')      : JText::_('JULY');
 273              case 8:  return $abbr ? JText::_('AUGUST_SHORT')    : JText::_('AUGUST');
 274              case 9:  return $abbr ? JText::_('SEPTEMBER_SHORT')  : JText::_('SEPTEMBER');
 275              case 10: return $abbr ? JText::_('OCTOBER_SHORT')   : JText::_('OCTOBER');
 276              case 11: return $abbr ? JText::_('NOVEMBER_SHORT')  : JText::_('NOVEMBER');
 277              case 12: return $abbr ? JText::_('DECEMBER_SHORT')  : JText::_('DECEMBER');
 278          }
 279      }
 280  
 281      /**
 282       * Translates day of week number to string
 283       *
 284       * @access protected
 285       * @param int $day The numeric day of the week
 286       * @param bool $abbr Return the abreviated day string?
 287       * @return string day string
 288       */
 289  	function _dayToString($day, $abbr = false)
 290      {
 291          switch ($day)
 292          {
 293              case 0: return $abbr ? JText::_('SUN') : JText::_('SUNDAY');
 294              case 1: return $abbr ? JText::_('MON') : JText::_('MONDAY');
 295              case 2: return $abbr ? JText::_('TUE') : JText::_('TUESDAY');
 296              case 3: return $abbr ? JText::_('WED') : JText::_('WEDNESDAY');
 297              case 4: return $abbr ? JText::_('THU') : JText::_('THURSDAY');
 298              case 5: return $abbr ? JText::_('FRI') : JText::_('FRIDAY');
 299              case 6: return $abbr ? JText::_('SAT') : JText::_('SATURDAY');
 300          }
 301      }
 302  
 303  }


Generated: Wed Mar 28 15:54:07 2012 Cross-referenced by PHPXref 0.7.1