00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00021 class Timestamp extends Date
00022 {
00023 private $hour = null;
00024 private $minute = null;
00025 private $second = null;
00026
00030 public static function create($timestamp)
00031 {
00032 return new self($timestamp);
00033 }
00034
00035 public static function now()
00036 {
00037 return date(self::getFormat());
00038 }
00039
00043 public static function makeNow()
00044 {
00045 return new self(time());
00046 }
00047
00051 public static function makeToday()
00052 {
00053 return new self(self::today());
00054 }
00055
00056 public function toTime($timeDelimiter = ':', $secondDelimiter = '.')
00057 {
00058 return
00059 $this->hour
00060 .$timeDelimiter
00061 .$this->minute
00062 .$secondDelimiter
00063 .$this->second;
00064 }
00065
00066 public function toDateTime(
00067 $dateDelimiter = '-',
00068 $timeDelimiter = ':',
00069 $secondDelimiter = '.'
00070 )
00071 {
00072 return
00073 $this->toDate($dateDelimiter).' '
00074 .$this->toTime($timeDelimiter, $secondDelimiter);
00075 }
00076
00077 public function getHour()
00078 {
00079 return $this->hour;
00080 }
00081
00082 public function getMinute()
00083 {
00084 return $this->minute;
00085 }
00086
00087 public function getSecond()
00088 {
00089 return $this->second;
00090 }
00091
00092 public function equals(Timestamp $timestamp)
00093 {
00094 return ($this->toDateTime() === $timestamp->toDateTime());
00095 }
00096
00097 public function getDayStartStamp()
00098 {
00099 if (!$this->hour && !$this->minute && !$this->second)
00100 return $this->int;
00101 else
00102 return parent::getDayStartStamp();
00103 }
00104
00105 public function getHourStartStamp()
00106 {
00107 if (!$this->minute && !$this->second)
00108 return $this->int;
00109
00110 return
00111 mktime(
00112 $this->hour,
00113 0,
00114 0,
00115 $this->month,
00116 $this->day,
00117 $this->year
00118 );
00119 }
00120
00124 public function toIsoString($convertToUtc = true)
00125 {
00126 if ($convertToUtc)
00127 return date('Y-m-d\TH:i:s\Z', $this->int - date('Z', $this->int));
00128 else
00129 return date('Y-m-d\TH:i:sO', $this->int);
00130 }
00131
00135 public function toTimestamp()
00136 {
00137 return $this;
00138 }
00139
00140 protected static function getFormat()
00141 {
00142 return 'Y-m-d H:i:s';
00143 }
00144
00145 protected function import($string)
00146 {
00147 list($date, $time) = explode(' ', $string, 2);
00148
00149 list($this->hour, $this->minute, $this->second) =
00150 explode(':', $time, 3);
00151
00152 $time =
00153 sprintf(
00154 '%02d:%02d:%02d',
00155 $this->hour,
00156 $this->minute,
00157 $this->second
00158 );
00159
00160 list($this->hour, $this->minute, $this->second) =
00161 explode(':', $time, 3);
00162
00163 parent::import($date);
00164
00165 $this->string .= ' '.$time;
00166 }
00167
00168 protected function stringImport($string)
00169 {
00170 $matches = array();
00171
00172 if (
00173 preg_match(
00174 '/^(\d{1,4})-(\d{1,2})-(\d{1,2})\s\d{1,2}:\d{1,2}:\d{1,2}$/',
00175 $string,
00176 $matches
00177 )
00178 ) {
00179 if (checkdate($matches[2], $matches[3], $matches[1]))
00180 $this->string = $string;
00181 } elseif (
00182 preg_match(
00183 '/^(\d{1,4})-(\d{1,2})-(\d{1,2})$/',
00184 $string,
00185 $matches
00186 )
00187 ) {
00188 if (checkdate($matches[2], $matches[3], $matches[1]))
00189 $this->string = $string . ' 00:00:00';
00190 } elseif (($stamp = strtotime($string)) !== false)
00191 $this->string = date($this->getFormat(), $stamp);
00192 }
00193
00194 protected function buildInteger()
00195 {
00196 $this->int =
00197 mktime(
00198 $this->hour,
00199 $this->minute,
00200 $this->second,
00201 $this->month,
00202 $this->day,
00203 $this->year
00204 );
00205 }
00206 }
00207 ?>