Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/utilities/date.php

Documentation is available at date.php

  1. <?php
  2. /**
  3. @version        $Id: date.php 6703 2007-02-23 03:23:32Z CoolAcid $
  4. @package        Joomla.Framework
  5. @subpackage    Utilities
  6. @copyright    Copyright (C) 2005 - 2007 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. if(!defined('DATE_FORMAT_LC')) {
  19.     define('DATE_FORMAT_LC''%A, %d %B %Y');
  20. }
  21.  
  22. if(!defined('DATE_FORMAT_LC2')) {
  23.     define('DATE_FORMAT_LC2''%A, %d %B %Y %H:%M');
  24. }
  25.  
  26. if(!defined('DATE_FORMAT_LC3')) {
  27.     define('DATE_FORMAT_LC3''%d %B %Y');
  28. }
  29.  
  30. if(!defined('DATE_FORMAT_LC4')) {
  31.     define('DATE_FORMAT_LC4''%d.%m.%y');
  32. }
  33.  
  34. /**
  35.  * JDate is a class that stores a date
  36.  *
  37.  * @author    Johan Janssens <[email protected]>
  38.  *
  39.  * @package        Joomla.Framework
  40.  * @subpackage    Utilities
  41.  * @since        1.5
  42.  */
  43. class JDate extends JObject
  44. {
  45.     /**
  46.      * Unix timestamp
  47.      *
  48.      * @var        string 
  49.      * @access    protected
  50.      */
  51.     var $_date = 0;
  52.  
  53.     /**
  54.      * Timeoffset (in hours)
  55.      *
  56.      * @var        string 
  57.      * @access    protected
  58.      */
  59.     var $_offset = 0;
  60.  
  61.     /**
  62.      * Creates a new instance of JDate representing a given date.
  63.      *
  64.      * Accepts RFC 822, ISO 8601 date formats as well as unix time stamps.
  65.      * If not specified, the current date and time is used.
  66.      *
  67.      * @param mixed $date optional the date this JDate will represent.
  68.      */
  69.     function __construct($date 'now'$tzOffset 0)
  70.     {
  71.  
  72.         if ($date == 'now' || empty($date))
  73.         {
  74.             $this->_date = gmdate('U');
  75.             return;
  76.         }
  77.  
  78.         if (is_numeric($date))
  79.         {
  80.             $this->_date = $date ($this->_offset * 3600);
  81.             return;
  82.         }
  83.  
  84.         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+(.*)~",$date,$matches))
  85.         {
  86.             $months Array("Jan"=>1,"Feb"=>2,"Mar"=>3,"Apr"=>4,"May"=>5,"Jun"=>6,"Jul"=>7,"Aug"=>8,"Sep"=>9,"Oct"=>10,"Nov"=>11,"Dec"=>12);
  87.             $this->_date = gmmktime($matches[4],$matches[5],$matches[6],$months[$matches[2]],$matches[1],$matches[3]);
  88.  
  89.             if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-'{
  90.                 $tzOffset (substr($matches[7],0,360 substr($matches[7],-2)) 60;
  91.             else {
  92.                 if (strlen($matches[7])==1{
  93.                     $oneHour 3600;
  94.                     $ord ord($matches[7]);
  95.                     if ($ord ord("M")) {
  96.                         $tzOffset (ord("A"$ord 1$oneHour;
  97.                     elseif ($ord >= ord("M"AND $matches[7]!="Z"{
  98.                         $tzOffset ($ord ord("M")) $oneHour;
  99.                     elseif ($matches[7]=="Z"{
  100.                         $tzOffset 0;
  101.                     }
  102.                 }
  103.                 switch ($matches[7]{
  104.                     case "UT":
  105.                     case "GMT":    $tzOffset 0;
  106.                 }
  107.             }
  108.             $this->_date -= $tzOffset;
  109.             return;
  110.         }
  111.         if (preg_match("~(\\d{4})-(\\d{2})-(\\d{2})T(\\d{2}):(\\d{2}):(\\d{2})(.*)~",$date,$matches))
  112.         {
  113.             $this->_date = gmmktime($matches[4],$matches[5],$matches[6],$matches[2],$matches[3],$matches[1]);
  114.             if (substr($matches[7],0,1)=='+' OR substr($matches[7],0,1)=='-'{
  115.                 $tzOffset (substr($matches[7],0,360 substr($matches[7],-2)) 60;
  116.             else {
  117.                 if ($matches[7]=="Z"{
  118.                     $tzOffset 0;
  119.                 }
  120.             }
  121.             $this->_date -= $tzOffset;
  122.             return;
  123.         }
  124.  
  125.         $this->_date = strtotime($date$this->serverOffset(($tzOffset*3600);
  126.     }
  127.  
  128.     /**
  129.      * Set the date offset (in hours)
  130.      *
  131.      * @access public
  132.      * @param integer $offset The offset in hours
  133.      */
  134.     function setOffset($offset{
  135.         $this->_offset = $offset;
  136.     }
  137.  
  138.     /**
  139.      * Get the date offset (in hours)
  140.      *
  141.      * @access public
  142.      * @return integer 
  143.      */
  144.     function getOffset({
  145.         return $this->_offset;
  146.     }
  147.  
  148.     /**
  149.      * Gets the date as an RFC 822 date.
  150.      *
  151.      * @return date in RFC 822 format
  152.      * @link http://www.ietf.org/rfc/rfc2822.txt?number=2822 IETF RFC 2822 (replaces RFC 822)
  153.      */
  154.     function toRFC822()
  155.     {
  156.         $date date("D, d M Y H:i:s O"$this->_date);
  157.         return $date;
  158.     }
  159.  
  160.     /**
  161.      * Gets the date as an ISO 8601 date.
  162.      *
  163.      * @return date in ISO 8601 (RFC 3339) format
  164.      * @link http://www.ietf.org/rfc/rfc3339.txt?number=3339 IETF RFC 3339
  165.      */
  166.     function toISO8601()
  167.     {
  168.         $date date("Y-m-d\TH:i:sP"$this->_date);
  169.         return $date;
  170.     }
  171.  
  172.     /**
  173.      * Gets the date as in MySQL datetime format
  174.      *
  175.      * @return date in MySQL datetime format
  176.      * @link http://dev.mysql.com/doc/refman/4.1/en/datetime.html MySQL DATETIME format
  177.      */
  178.     function toMySQL()
  179.     {
  180.         $date gmdate("Y-m-d H:i:s"$this->_date);
  181.         return $date;
  182.     }
  183.  
  184.     /**
  185.      * Gets the date as UNIX time stamp.
  186.      *
  187.      * @return date as a unix time stamp
  188.      */
  189.     function toUnix()
  190.     {
  191.         $date =  $this->_date;
  192.         return $date;
  193.     }
  194.  
  195.     /**
  196.      * Gets the date in a specific format
  197.      *
  198.      * Returns a string formatted according to the given format. Month and weekday names and
  199.      * other language dependent strings respect the current locale
  200.      *
  201.      * @param string $format  The date format specification string (see {@link PHP_MANUAL#strftime})
  202.      * @return date in a specific format
  203.      */
  204.     function toFormat($format '%Y-%m-%d %H:%M:%S')
  205.     {
  206.         $date gmstrftime($format$this->_date + ($this->_offset * 3600));
  207.  
  208.         // for Windows there is a need to convert the OS date string to utf-8.
  209.         $lang =JFactory::getLanguage();
  210.         if JUtility::isWinOS(&& function_exists('iconv') ) {
  211.             return iconv($lang->getWinCP()"UTF-8"$date);
  212.         }
  213.  
  214.         return $date;
  215.     }
  216.  
  217.     function serverOffset()
  218.     {
  219.         $tz date('O');
  220.  
  221.         $tzOffset ((intval(substr($tz,1,2))*60intval(substr($tz,-2)))*60;
  222.         if (substr($tz,0,1== '-')
  223.         {
  224.         $tzOffset = -$tzOffset;
  225.         }
  226.  
  227.         return $tzOffset;
  228.     }
  229.  
  230. }
  231. ?>

Documentation generated on Mon, 05 Mar 2007 20:56:20 +0000 by phpDocumentor 1.3.1