Advertisement
Guest User

TzDate: time/date manipulation with timezones

a guest
Mar 30th, 2011
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.94 KB | None | 0 0
  1. <?php
  2.  
  3. class TzDate {
  4.  
  5.     private static $_default_tz = 'UTC';
  6.     private static $_default_format = 'r';
  7.  
  8.     private $_tz;
  9.     private $_timestamp;
  10.  
  11.     /**
  12.      * Returns instance of this class
  13.      *
  14.      * @param string (optional) Date value to create class from
  15.      * @param string (optional) Valid time zone identifier
  16.      * @param bool (optional) Use specified time zone for output (true: yes)
  17.      * @return object Instance of this class
  18.      */
  19.     public static function factory($date = null, $tz = null, $set_timezone = false)
  20.     {
  21.         $class = __CLASS__;
  22.         return new $class($date, $tz, $set_timezone);
  23.     }
  24.  
  25.     /**
  26.      * Set default output time zone for this class and returns it
  27.      *
  28.      * @param string Valid time zone identifier
  29.      * @return string
  30.      */
  31.     public static function default_timezone($tz = null)
  32.     {
  33.         if ($tz !== null)
  34.         {
  35.             self::$_default_tz = $tz;
  36.         }
  37.  
  38.         return self::$_default_tz;
  39.     }
  40.  
  41.     /**
  42.      * Set default date format string used in this class and returns it
  43.      *
  44.      * @param string Valid date() format string
  45.      * @return string
  46.      */
  47.     public static function default_format($format = null)
  48.     {
  49.         if ($format !== null)
  50.         {
  51.             self::$_default_format = $format;
  52.         }
  53.  
  54.         return self::$_default_format;
  55.     }
  56.  
  57.     /**
  58.      * Creates new instance of this class, (optionally) using specified date,
  59.      * time zone (which, if specified, helps to correct given date back to UTC
  60.      *
  61.      * @param string (optional) Date value to create class from
  62.      * @param string (optional) Valid time zone identifier
  63.      * @param bool (optional) Use specified time zone for output (true: yes)
  64.      */
  65.     public function __construct($date = null, $tz = null, $set_timezone = false)
  66.     {
  67.         if ($set_timezone)
  68.         {
  69.             $this->timezone($tz);
  70.         }
  71.  
  72.         if ($date !== null)
  73.         {
  74.             $this->set($date, $tz);
  75.         }
  76.     }
  77.  
  78.     /**
  79.      * Set date stored in this object, using specified date and (optionally)
  80.      * specified time zone
  81.      *
  82.      * @param string Date value to set in this object (can be relative value)
  83.      * @param string (optional) Valid time zone identifier
  84.      * @return object $this to allow method chaining
  85.      */
  86.     public function set($date, $tz = null)
  87.     {
  88.         if ($this->date_has_tz_info($date))
  89.         {
  90.             $date = $this->strip_tz_info($date);
  91.         }
  92.  
  93.         $this->adjust($date, $tz);
  94.  
  95.         return $this;
  96.     }
  97.  
  98.     /**
  99.      * Adjusts this object's date value by specified value, considering
  100.      * specified time zone (so that given value is relative to active time
  101.      * zone setting)
  102.      *
  103.      * Value can be any valid strtotime() identifier.
  104.      *
  105.      * @param string Valid strtotime() identifier
  106.      * @param string (optional) Valid time zone identifier
  107.      * @return object $this to allow method chaining
  108.      */
  109.     public function adjust($value, $tz = null)
  110.     {
  111.         if ($tz !== null)
  112.         {
  113.             $tz = new DateTimeZone($tz);
  114.         }
  115.         else
  116.         {
  117.             $tz = new DateTimeZone(date_default_timezone_get());
  118.         }
  119.  
  120.         $dt = null;
  121.  
  122.         if ($this->_timestamp)
  123.         {
  124.             $dt = new DateTime('@'.$this->_timestamp, $tz);
  125.             $dt->modify($value);
  126.         }
  127.         else
  128.         {
  129.             $dt = new DateTime($value, $tz);
  130.         }
  131.  
  132.         $this->_timestamp = $dt->format('U');
  133.  
  134.         return $this;
  135.     }
  136.  
  137.     /**
  138.      * Sets output time zone. This time zone is used when date is output from
  139.      * this object.
  140.      *
  141.      * @param string Valid time zone identifier
  142.      * @return object $this to allow method chaining
  143.      */
  144.     public function timezone($tz)
  145.     {
  146.         if (is_object($tz) and $tz instanceof DateTimeZone)
  147.         {
  148.             $this->_tz = $tz->getName();
  149.         }
  150.         else if ($tz !== null)
  151.         {
  152.             $this->_tz = $tz;
  153.         }
  154.  
  155.         return $this;
  156.     }
  157.  
  158.     /**
  159.      * Returns date in specified time zone, using default date format.
  160.      *
  161.      * @param string Valid time zone identifier
  162.      * @return string
  163.      */
  164.     public function convert($tz)
  165.     {
  166.         return $this->format($this->default_format, $tz);
  167.     }
  168.  
  169.     /**
  170.      * Returns date in specified format and (optionally) specified time zone.
  171.      *
  172.      * @param string Valid date() format string
  173.      * @param string (optional) Valid time zone identifier
  174.      * @return string
  175.      */
  176.     public function format($format, $tz = null)
  177.     {
  178.         if ($tz === null)
  179.         {
  180.             $tz = $this->timezone;
  181.         }
  182.  
  183.         $dto = new DateTime('@'.($this->_timestamp ? $this->_timestamp : 0));
  184.         $dto->setTimezone(new DateTimeZone($tz));
  185.  
  186.         return $dto->format($format);
  187.     }
  188.  
  189.     /**
  190.      * Checks if passed date contains time zone identifier.
  191.      *
  192.      * Note: This method is twice as fast as it would have been if it was
  193.      * written using preg_match method! (tested and confirmed.)
  194.      *
  195.      * @param string Date string to test
  196.      * @return bool
  197.      */
  198.     private function date_has_tz_info($date_string)
  199.     {
  200.         if (
  201.             substr($date_string, -1) == 'z' // date ends with Z, meaning UTC (US NAVY spelling: Zulu)
  202.             or ($sign = substr($date_string, -6, 1) and ($sign == '+' or $sign == '-')) // ends with +dd:dd
  203.             or ($sign = substr($date_string, -5, 1) and ($sign == '+' or $sign == '-')) // ends with +dddd
  204.         )
  205.         {
  206.             return true;
  207.         }
  208.         else
  209.         {
  210.             return false;
  211.         }
  212.     }
  213.  
  214.     /**
  215.      * Strips time zone information embeded in date string.
  216.      *
  217.      * Examples:
  218.      * - 2010-01-01T20:00:13Z (removes "Z")
  219.      * - 2010-01-01T20:00:13+0100 (removes "+0100")
  220.      * - 2010-01-01 20:00:13 -0300 (removes " -0300")
  221.      *
  222.      * @param string Date string
  223.      * @return string
  224.      */
  225.     private function strip_tz_info($date_string)
  226.     {
  227.         return preg_replace('/\s*(z|[\+\-](\d{2}:\d{2}|\d{2,4}))$/i', '', $date_string);
  228.     }
  229.  
  230.     /**
  231.      * Public getter for current class.
  232.      *
  233.      * Properties defined:
  234.      * - timezone (or tz): returns output timezone
  235.      * - timestamp: returns current timestamp value
  236.      * - default_format: returns default date formatting string
  237.      * - date: returns date stored in this object, using default format
  238.      *
  239.      * @return string
  240.      */
  241.     public function __get($column)
  242.     {
  243.         if ($column == 'timezone' or $column == 'tz')
  244.         {
  245.             if ($this->_tz)
  246.             {
  247.                 return $this->_tz;
  248.             }
  249.             else
  250.             {
  251.                 return self::$_default_tz;
  252.             }
  253.         }
  254.         else if ($column == 'timestamp')
  255.         {
  256.             return $this->_timestamp;
  257.         }
  258.         else if ($column == 'default_format')
  259.         {
  260.             return self::$_default_format;
  261.         }
  262.         else if ($column == 'date')
  263.         {
  264.             return $this->format($this->default_format);
  265.         }
  266.     }
  267.  
  268.     /**
  269.      * Public setter for this class.
  270.      *
  271.      * Properties defined:
  272.      * - timezone (or tz): sets output timezone
  273.      * - date: sets date stored in this object
  274.      *
  275.      * @return void
  276.      */
  277.     public function __set($column, $value)
  278.     {
  279.         if ($column == 'timezone' or $column == 'tz')
  280.         {
  281.             $this->timezone($value);
  282.         }
  283.         else if ($column == 'date')
  284.         {
  285.             $this->set($value);
  286.         }
  287.     }
  288.  
  289.     /**
  290.      * Returns string representation of this object, which is the date
  291.      * contained in it, using default date format, and selected (or default)
  292.      * output time zone setting
  293.      *
  294.      * @return string
  295.      */
  296.     public function __toString()
  297.     {
  298.         return $this->date;
  299.     }
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement