Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

eztime.php
Go to the documentation of this file.
1 <?php
58 class eZTime
59 {
60 public $IsValid;
64 const SECONDS_A_MINUTE = 60;
65
69 const SECONDS_AN_HOUR = 3600;
70
74 const SECONDS_A_DAY = 86400; // 24*60*60
75
81 public function __construct( $timestamp = false )
82 {
83 if ( $timestamp === false )
84 {
85 $cur_date = getdate();
86 $this->setHMS( $cur_date[ 'hours' ],
87 $cur_date[ 'minutes' ],
88 $cur_date[ 'seconds' ] );
89 }
90 else if ( $timestamp > self::SECONDS_A_DAY )
91 $this->setTimeStamp( $timestamp );
92 else
93 $this->setTimeOfDay( $timestamp );
94
95 $this->Locale = eZLocale::instance();
96 }
97
98 function attributes()
99 {
100 return array( 'timestamp',
101 'time_of_day',
102 'hour',
103 'minute',
104 'second',
105 'is_valid' );
106 }
107
108 function hasAttribute( $name )
109 {
110 return in_array( $name, $this->attributes() );
111 }
112
113 function attribute( $name )
114 {
115 if ( $name == 'timestamp' )
116 return $this->timeStamp();
117 else if ( $name == 'time_of_day' )
118 return $this->timeOfDay();
119 else if ( $name == 'hour' )
120 return $this->hour();
121 else if ( $name == 'minute' )
122 return $this->minute();
123 else if ( $name == 'second' )
124 return $this->second();
125 else if ( $name == 'is_valid' )
126 return $this->isValid();
127
128 eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
129 return false;
130 }
131
135 function isValid()
136 {
137 return $this->IsValid;
138 }
139
143 function setLocale( &$locale )
144 {
145 $this->Locale =& $locale;
146 }
147
151 function &locale()
152 {
153 return $this->Locale;
154 }
155
159 function setHour( $hour )
160 {
161 $this->Time = ($hour % 24) * self::SECONDS_AN_HOUR + $this->minute() * self::SECONDS_A_MINUTE + $this->second();
162 $this->IsValid = $this->Time >= 0;
163 }
164
168 function setMinute( $min )
169 {
170 $this->Time = $this->hour() * self::SECONDS_AN_HOUR + ($min % 60) * self::SECONDS_A_MINUTE + $this->second();
171 $this->IsValid = $this->Time >= 0;
172 }
173
177 function setSecond( $sec )
178 {
179 $this->Time = $this->hour() * self::SECONDS_AN_HOUR + $this->minute() * self::SECONDS_A_MINUTE + ($sec % 60);
180 $this->IsValid = $this->Time >= 0;
181 }
182
186 function setHMS( $hour, $min = 0, $sec = 0 )
187 {
188 $this->Time = ($hour % 24 ) * self::SECONDS_AN_HOUR + ($min % 60) * self::SECONDS_A_MINUTE + ($sec % 60);
189 $this->IsValid = $this->Time >= 0;
190 }
191
195 function hour()
196 {
197 return (int)($this->Time / self::SECONDS_AN_HOUR);
198 }
199
203 function minute()
204 {
205 return (int)(($this->Time % self::SECONDS_AN_HOUR) / self::SECONDS_A_MINUTE);
206 }
207
211 function second()
212 {
213 return (int)($this->Time % self::SECONDS_A_MINUTE);
214 }
215
216 /*
217 Return long timestamp ($this->Time + current day midnight timestamp)
218 */
219 function timeStamp()
220 {
221 $cur_date = getdate();
222 $longtimestamp = mktime( $this->hour(),
223 $this->minute(),
224 $this->second(),
225 $cur_date[ 'mon' ],
226 $cur_date[ 'mday' ],
227 $cur_date[ 'year' ] );
228 return $longtimestamp;
229 }
230
231 /*
232 Sets timestamp (time of the das) by clamping off time from the last midnight.
233 */
234 function setTimeStamp( $timestamp )
235 {
236 $date = getdate( $timestamp );
237 $this->Time = $date[ 'hours' ] * self::SECONDS_AN_HOUR +
238 $date[ 'minutes' ] * self::SECONDS_A_MINUTE +
239 $date[ 'seconds' ];
240 }
241
246 function timeOfDay()
247 {
248 return $this->Time;
249 }
250
251 /*
252 Sets time of day (the number of seconds since begining of day)
253 */
254 function setTimeOfDay( $timestamp )
255 {
256 $this->Time = $timestamp % self::SECONDS_A_DAY;
257 if ( $this->Time < 0 )
258 $this->Time += self::SECONDS_A_DAY;
259 $this->IsValid = $this->Time >= 0;
260 }
261
262
267 function adjustTime( $hour, $minute = 0, $second = 0 )
268 {
269 $this->setTimeOfDay( $hour * self::SECONDS_AN_HOUR +
270 $minute * self::SECONDS_A_MINUTE +
271 $second + $this->Time );
272 }
273
278 static function create( $hour = -1, $minute = -1, $second = -1 )
279 {
280 $cur_date = getdate();
281
282 $time = new eZTime();
283 $time->setHMS( $hour < 0 ? $cur_date[ 'hours' ] : $hour,
284 $minute < 0 ? $cur_date[ 'minutes' ] : $minute,
285 $second < 0 ? $cur_date[ 'seconds' ] : $second );
286 return $time;
287 }
288
293 function duplicate()
294 {
295 $copy = clone $this;
296 return $copy;
297 }
298
304 function isGreaterThan( &$time, $equal = false )
305 {
306 $t1 =& $this->Time;
307 if ( $time instanceof eZTime )
308 $t2 = $time->timeOfDay();
309 else
310 $t2 = ( $time % self::SECONDS_A_DAY );
311 if ( $t1 > $t2 )
312 return true;
313 else if ( $equal and $t1 == $t2 )
314 return true;
315 else
316 return false;
317 }
318
323 function isEqualTo( &$time )
324 {
325 $t1 =& $this->Time;
326 if ( $time instanceof eZTime )
327 $t2 = $time->timeOfDay();
328 else
329 $t2 = ( $time % self::SECONDS_A_DAY );
330 return $t1 == $t2;
331 }
332
337 function toString( $short = false )
338 {
339 if ( $short )
340 $str = $this->Locale->formatShortTime( $this->timeStamp() );
341 else
342 $str = $this->Locale->formatTime( $this->timeStamp() );
343 return $str;
344 }
345
351 static function secondsPerDay()
352 {
353 return self::SECONDS_A_DAY;
354 }
355
357 public $Locale;
359 public $Time;
360 }
361
362 ?>
eZDebug\writeError
static writeError( $string, $label="", $backgroundClass="")
Definition: ezdebug.php:545
eZLocale\instance
static instance( $localeString=false)
Definition: ezlocale.php:1536
eZTime
Locale aware time handler.
Definition: eztime.php:59
eZTime\setMinute
setMinute( $min)
Definition: eztime.php:168
eZTime\$Locale
$Locale
Locale object, is just a reference to minimize memory usage.
Definition: eztime.php:357
eZTime\minute
minute()
Definition: eztime.php:203
eZTime\isValid
isValid()
Definition: eztime.php:135
eZTime\setLocale
setLocale(&$locale)
Definition: eztime.php:143
eZTime\setSecond
setSecond( $sec)
Definition: eztime.php:177
eZTime\toString
toString( $short=false)
Definition: eztime.php:337
eZTime\timeStamp
timeStamp()
Definition: eztime.php:219
eZTime\setHour
setHour( $hour)
Definition: eztime.php:159
eZTime\attributes
attributes()
Definition: eztime.php:98
eZTime\duplicate
duplicate()
Definition: eztime.php:293
eZTime\setTimeStamp
setTimeStamp( $timestamp)
Definition: eztime.php:234
eZTime\timeOfDay
timeOfDay()
Definition: eztime.php:246
eZTime\SECONDS_A_DAY
const SECONDS_A_DAY
Definition: eztime.php:74
eZTime\isEqualTo
isEqualTo(&$time)
Definition: eztime.php:323
eZTime\secondsPerDay
static secondsPerDay()
Definition: eztime.php:351
eZTime\create
static create( $hour=-1, $minute=-1, $second=-1)
Definition: eztime.php:278
eZTime\SECONDS_A_MINUTE
const SECONDS_A_MINUTE
Definition: eztime.php:64
eZTime\hasAttribute
hasAttribute( $name)
Definition: eztime.php:108
eZTime\hour
hour()
Definition: eztime.php:195
eZTime\locale
& locale()
Definition: eztime.php:151
eZTime\SECONDS_AN_HOUR
const SECONDS_AN_HOUR
Definition: eztime.php:69
eZTime\$IsValid
$IsValid
Definition: eztime.php:60
eZTime\setTimeOfDay
setTimeOfDay( $timestamp)
Definition: eztime.php:254
eZTime\adjustTime
adjustTime( $hour, $minute=0, $second=0)
Definition: eztime.php:267
eZTime\second
second()
Definition: eztime.php:211
eZTime\attribute
attribute( $name)
Definition: eztime.php:113
eZTime\isGreaterThan
isGreaterThan(&$time, $equal=false)
Definition: eztime.php:304
eZTime\__construct
__construct( $timestamp=false)
Definition: eztime.php:81
eZTime\setHMS
setHMS( $hour, $min=0, $sec=0)
Definition: eztime.php:186
eZTime\$Time
$Time
The current time as a clamped timestamp.
Definition: eztime.php:359
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.