[ PHPXref.com ] [ Generated: Sun Jul 20 19:05:09 2008 ] [ onPHP 0.4.6 ]
[ Index ]     [ Variables ]     [ Functions ]     [ Classes ]     [ Constants ]     [ Statistics ]

title

Body

[close]

/core/Base/ -> Timestamp.class.php (source)

   1  <?php
   2  /***************************************************************************
   3   *   Copyright (C) 2004-2006 by Garmonbozia Research Group,                *
   4   *   Anton E. Lebedevich, Konstantin V. Arkhipov                           *
   5   *                                                                         *
   6   *   This program is free software; you can redistribute it and/or modify  *
   7   *   it under the terms of the GNU General Public License as published by  *
   8   *   the Free Software Foundation; either version 2 of the License, or     *
   9   *   (at your option) any later version.                                   *
  10   *                                                                         *
  11   ***************************************************************************/
  12  /* $Id: Timestamp.class.php 1684 2006-06-10 20:58:39Z voxus $ */
  13  
  14      /**
  15       * Date's container and utilities.
  16       *
  17       * @see DateRange
  18       * 
  19       * @ingroup Base
  20      **/
  21      class Timestamp implements Stringable
  22      {
  23          private $string        = null;
  24          private $int        = null;
  25          
  26          private $year        = null;
  27          private $month        = null;
  28          private $day        = null;
  29          
  30          private $hour        = null;
  31          private $minute        = null;
  32          private $second        = null;
  33  
  34          const WEEKDAY_MONDAY     = 1;
  35          const WEEKDAY_TUESDAY    = 2;
  36          const WEEKDAY_WEDNESDAY    = 3;
  37          const WEEKDAY_THURSDAY    = 4;
  38          const WEEKDAY_FRIDAY    = 5;
  39          const WEEKDAY_SATURDAY    = 6;
  40          const WEEKDAY_SUNDAY    = 7;
  41          
  42  		public function __construct($timestamp)
  43          {
  44              if (is_int($timestamp)) { // unix timestamp
  45                  $this->int = $timestamp;
  46                  $this->string = date('Y-m-d H:i:s', $timestamp);
  47              } elseif (is_string($timestamp)) { 
  48                  $this->int = strtotime($timestamp);
  49  
  50                  if (
  51                      preg_match(
  52                          '/^\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}$/',
  53                          $timestamp
  54                      )
  55                  )
  56                      $this->string = $timestamp;
  57                  elseif (preg_match('/^\d{4}-\d{2}-\d{2}$/', $timestamp))
  58                      $this->string = $timestamp . ' 00:00:00';
  59                  else
  60                      $this->string = date('Y-m-d H:i:s', $this->int);
  61  
  62              } else {
  63                  throw new WrongArgumentException(
  64                      "strange timestamp given - '{$timestamp}'"
  65                  );
  66              }
  67              
  68              $this->import($this->string);
  69          }
  70          
  71  		public static function create($timestamp)
  72          {
  73              return new Timestamp($timestamp);
  74          }
  75  
  76  		public function toStamp()
  77          {
  78              return $this->int;
  79          }
  80          
  81  		public function toDate($delimiter = '-')
  82          {
  83              return
  84                  $this->year
  85                  .$delimiter
  86                  .$this->month
  87                  .$delimiter
  88                  .$this->day;
  89          }
  90          
  91  		public function toTime($timeDelimiter = ':', $secondDelimiter = '.')
  92          {
  93              return
  94                  $this->hour
  95                  .$timeDelimiter
  96                  .$this->minute
  97                  .$secondDelimiter
  98                  .$this->second;
  99          }
 100          
 101  		public function toDateTime(
 102              $dateDelimiter = '-',
 103              $timeDelimiter = ':',
 104              $secondDelimiter = '.'
 105          )
 106          {
 107              return
 108                  $this->toDate($dateDelimiter).' '
 109                  .$this->toTime($timeDelimiter, $secondDelimiter);
 110          }
 111  
 112  		public function getYear()
 113          {
 114              return $this->year;
 115          }
 116  
 117  		public function getMonth()
 118          {
 119              return $this->month;
 120          }
 121  
 122  		public function getDay()
 123          {
 124              return $this->day;
 125          }
 126  
 127  		public function getWeekDay()
 128          {
 129              return strftime('%w', $this->int);
 130          }
 131          
 132  		public function getHour()
 133          {
 134              return $this->hour;
 135          }
 136          
 137  		public function getMinute()
 138          {
 139              return $this->minute;
 140          }
 141          
 142  		public function spawn($modification = null)
 143          {
 144              $child = new Timestamp($this->string);
 145              
 146              if ($modification)
 147                  return $child->modify($modification);
 148              else
 149                  return $child;
 150          }
 151          
 152  		public function modify($string)
 153          {
 154              try {
 155                  $time = strtotime($string, $this->int);
 156                  
 157                  if ($time === false)
 158                      throw new WrongArgumentException(
 159                          "modification yielded false '{$string}'"
 160                      );
 161                  
 162                  $this->int = $time;
 163                  $this->string = date('Y-m-d H:i:s', $time);
 164                  $this->import($this->string);
 165  
 166                  return $this;
 167              } catch (BaseException $e) {
 168                  throw new WrongArgumentException(
 169                      "wrong time string '{$string}'"
 170                  );
 171              }
 172          }
 173          
 174  		public function equals(Timestamp $timestamp)
 175          {
 176              return ($this->toDateTime() === $timestamp->toDateTime());
 177          }
 178          
 179  		public function toString()
 180          {
 181              return $this->string;
 182          }
 183          
 184          /**
 185           * @todo break API by returning Timestamp instance instead
 186          **/
 187  		public static function now()
 188          {
 189              return date('Y-m-d H:i:s');
 190          }
 191          
 192  		public static function today($delimiter = '-')
 193          {
 194              return date("Y{$delimiter}m{$delimiter}d");
 195          }
 196  
 197  		public function getDayStartStamp()
 198          {
 199              if (!$this->hour && !$this->minute && !$this->second)
 200                  return $this->int;
 201              else
 202                  return mktime(
 203                          0, 0, 0,
 204                          $this->month,
 205                          $this->day,
 206                          $this->year
 207                      );
 208          }
 209  
 210  		public function getDayEndStamp()
 211          {
 212              return mktime(
 213                      23, 59, 59,
 214                      $this->month,
 215                      $this->day,
 216                      $this->year
 217                  );
 218          }
 219          
 220  		private function import($string)
 221          {
 222              list($date, $time) = explode(' ', $string, 2);
 223              
 224              list($this->year, $this->month, $this->day) =
 225                  explode('-', $date, 3);
 226              
 227              list($this->hour, $this->minute, $this->second) =
 228                  explode(':', $time, 3);
 229          }
 230  
 231  		public static function compare(Timestamp $left, Timestamp $right)
 232          {
 233              if ($left->int == $right->int)
 234                  return 0;
 235              else
 236                  return ($left->int > $right->int ? 1 : -1);
 237          }
 238          
 239  		public function getFirstDayOfWeek($weekStart = Timestamp::WEEKDAY_MONDAY)
 240          {
 241              return $this->spawn(
 242                  '-'.((7 + $this->getWeekDay() - $weekStart) % 7).' days'
 243              );
 244          }
 245          
 246  		public function getLastDayOfWeek($weekStart = Timestamp::WEEKDAY_MONDAY)
 247          {
 248              return $this->spawn(
 249                  '+'.((13 - $this->getWeekDay() + $weekStart) % 7).' days'
 250              );
 251          }
 252      }
 253  ?>


[ Powered by PHPXref - Served by Debian GNU/Linux ]