Timestamp.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2004-2007 by Garmonbozia Research Group,                *
00004  *   Anton E. Lebedevich, Konstantin V. Arkhipov                           *
00005  *                                                                         *
00006  *   This program is free software; you can redistribute it and/or modify  *
00007  *   it under the terms of the GNU Lesser General Public License as        *
00008  *   published by the Free Software Foundation; either version 3 of the    *
00009  *   License, or (at your option) any later version.                       *
00010  *                                                                         *
00011  ***************************************************************************/
00012 /* $Id: Timestamp.class.php 4687 2007-12-09 18:57:18Z voxus $ */
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         
00108         public function toISOString($convertToUtc = true)
00109         {
00110             if ($convertToUtc)
00111                 return date('Y-m-d\TH:i:s\Z', $this->int - date('Z', $this->int));
00112             else
00113                 return date('Y-m-d\TH:i:sO', $this->int);
00114         }
00115         
00116         protected static function getFormat()
00117         {
00118             return 'Y-m-d H:i:s';
00119         }
00120         
00121         /* void */ protected function import($string)
00122         {
00123             list($date, $time) = explode(' ', $string, 2);
00124             
00125             parent::import($date);
00126             
00127             list($this->hour, $this->minute, $this->second) =
00128                 explode(':', $time, 3);
00129             
00130             $time =
00131                 sprintf(
00132                     '%02d:%02d:%02d',
00133                     $this->hour,
00134                     $this->minute,
00135                     $this->second
00136                 );
00137             
00138             list($this->hour, $this->minute, $this->second) =
00139                 explode(':', $time, 3);
00140             
00141             $this->string .= ' '.$time;
00142         }
00143         
00144         /* void */ protected function stringImport($string)
00145         {
00146             $this->int = strtotime($string);
00147             
00148             if (
00149                 preg_match(
00150                     '/^\d{1,4}-\d{1,2}-\d{1,2}\s\d{1,2}:\d{1,2}:\d{1,2}$/',
00151                     $string
00152                 )
00153             ) {
00154                 $this->string = $string;
00155             } elseif (preg_match('/^\d{1,4}-\d{1,2}-\d{1,2}$/', $string))
00156                 $this->string = $string . ' 00:00:00';
00157             elseif ($this->int !== false)
00158                 $this->string = date($this->getFormat(), $this->int);
00159         }
00160     }
00161 ?>

Generated on Sun Dec 9 21:56:23 2007 for onPHP by  doxygen 1.5.4