Source for file date.class.php

Documentation is available at date.class.php


1 <?php
2 /**
3 * contains class Date and other date funktions
4 *
5 * @category phpTimeSheet
6 * @package Date
7 * @version $Id: date.class.php,v 1.23 2003/09/25 09:35:56 cybot_tm Exp $
8 */
9
10 /**
11 *
12 */
13 define('DATE_EMPTY_IF_NULL', 0);
14
15
16 /**
17 * Class Date
18 *
19 * @package Date
20 * @author Sebastian Mendel <info@sebastianmendel.de>
21 * @version $Id: date.class.php,v 1.23 2003/09/25 09:35:56 cybot_tm Exp $
22 */
23 class Date
24 {
25 /**
26 * @var int day
27 */
28 var $day;
29
30 /**
31 * @var int month
32 */
33 var $month;
34
35 /**
36 * @var int year
37 */
38 var $year;
39
40 /**
41 * @var string century
42 */
43 var $century;
44
45 /**
46 * @param string date
47 * @since v1.0
48 */
49 function Date($date = null)
50 {
51 $this->Set($date);
52 return true;
53 }
54
55 /**
56 * return true if date is not 0 (0000-00-00), otherwise false
57 *
58 * @return bool true or false
59 * @since v1.3
60 */
61 function IsNull()
62 {
63 if ( $this->GetDay() + $this->GetMonth() + $this->GetYear() === 0 )
64 {
65 return true;
66 }
67 return false;
68 }
69
70 /**
71 * returns Date as string in ISO-Format (YYYY-MM-DD)
72 * @return string date
73 * @since v1.0
74 */
75 function Get() { return $this->GetAsIso(); }
76
77 /**
78 * returns day in month as int
79 * @return int day
80 * @since v1.0
81 */
82 function GetDay() { return $this->day; }
83
84 /**
85 * returns Month as int
86 * @return int month
87 * @since v1.0
88 */
89 function GetMonth() { return $this->month; }
90
91 /**
92 * returns Year as int
93 * @return int year
94 * @since v1.0
95 */
96 function GetYear() { return $this->year; }
97
98 /**
99 *
100 * @uses GetAsStr()
101 */
102 function GetWeekDay() { return (int) $this->GetAsStr('w'); }
103
104 /**
105 *
106 * @uses GetAsStr()
107 */
108 function GetWeek() { return (int) $this->GetAsStr('W'); }
109
110 /**
111 *
112 * @uses GetAsStr()
113 */
114 function GetDaysInMonth() { return (int) $this->GetAsStr('t'); }
115
116 /**
117 *
118 * @uses date()
119 */
120 function GetAsStr($format)
121 {
122 return date($format, $this->GetAsTs());
123 }
124
125 /**
126 * returns abbreviated month name according to the current locale
127 * @return int month
128 * @since v1.23
129 */
130 function GetLcMonthAbr() { return $this->GetAsLcStr('%b'); }
131
132 /**
133 * Returns a string formatted according to the given format string
134 *
135 * @since v1.23
136 * @param string format
137 * @see strftime()
138 * @uses strftime()
139 * @return string date
140 */
141 function GetAsLcStr($format)
142 {
143 return strftime($format, $this->GetAsTs());
144 }
145
146 /**
147 * returns Date as string in ISO-Format (YYYY-MM-DD)
148 * @return string date
149 * @since v1.0
150 */
151 function GetAsIso() { return sprintf("%04d-%02d-%02d", $this->GetYear(), $this->GetMonth(), $this->GetDay()); }
152
153 /**
154 * returns Date as string in DIN-Format (DD.MM.YYYY)
155 * if display_null = 1/true and IsNull() it returns 00.00.0000 otherwise an empty string
156 *
157 * @param int display_null
158 * @return string date
159 * @since v1.0
160 */
161 function GetAsDin($display_null = 1)
162 {
163 if ( $display_null === DATE_EMPTY_IF_NULL && $this->IsNull() )
164 {
165 return '';
166 }
167 else
168 {
169 return sprintf("%02d.%02d.%04d", $this->GetDay(), $this->GetMonth(), $this->GetYear());
170 }
171 }
172
173 /**
174 * returns Date as string in Ami-Format (mm/dd/yyyy)
175 * @return string date
176 * @since v1.0
177 */
178 function GetAsAmi() { return sprintf("%02d/%02d/%04d", $this->GetMonth(), $this->GetDay(), $this->GetYear()); }
179
180 /**
181 * returns Date as timestamp
182 * @return int timestamp
183 * @since v1.5
184 */
185 function GetAsTs() { return mktime(0, 0, 0, $this->GetMonth(), $this->GetDay(), $this->GetYear()); }
186
187 /**
188 * Sets date from given date-string, date-object or timestamp-int
189 * return true on success, otherwise false
190 *
191 * @param mixed date
192 * @return bool success
193 * @since v1.0
194 */
195 function Set($date)
196 {
197 if ( is_int($date) )
198 {
199 $this->SetFromTs($date);
200 }
201 elseif ( is_object($date) && get_class($date) == 'date' )
202 {
203 $this = $date;
204 }
205 elseif ( preg_match('|\.|', $date) )
206 {
207 $this->SetFromDin($date);
208 }
209 elseif ( preg_match('|\/|', $date) )
210 {
211 // date is in form m/d/y
212 $this->SetFromAmi($date);
213 }
214 elseif ( preg_match('|\-|', $date) )
215 {
216 // date is in form YYYY-MM-DD
217 $this->SetFromIso($date);
218 }
219 else
220 {
221 return false;
222 }
223
224 return true;
225 }
226
227 /**
228 * @param int day of month 0 to 31
229 * @since v1.0
230 */
231 function SetDay($day)
232 {
233 $this->day = (int) $day;
234 return true;
235 }
236
237 /**
238 * @param int month 0 to 12
239 * @since v1.0
240 */
241 function SetMonth($month)
242 {
243 $this->month = (int) $month;
244 return true;
245 }
246
247 /**
248 * @parameter int year with century
249 * @since v1.0
250 */
251 function SetYear($year)
252 {
253 $this->year = (int) $year;
254 return true;
255 }
256
257 /**
258 * Sets date from timestamp
259 *
260 * @param int timestamp
261 * @since v1.3
262 */
263 function SetFromTs($timestamp)
264 {
265 $this->SetDay(date('d', $timestamp));
266 $this->SetMonth(date('m', $timestamp));
267 $this->SetYear(date('Y', $timestamp));
268 }
269
270 /**
271 * Sets date from DIN-Format (DD.MM.YYYY or D.M.YY)
272 *
273 * @param string date
274 * @since v1.0
275 */
276 function SetFromDin($datestring)
277 {
278 $date = explode('.', $datestring);
279 if ( strlen($date[2]) === 2 )
280 {
281 $date[2] = ( ((int) strftime("%C")) * 100) + (int) $date[2];
282 }
283 $this->SetDay($date[0]);
284 $this->SetMonth($date[1]);
285 $this->SetYear($date[2]);
286 }
287
288 /**
289 * Sets date from USA-Format (mm/dd/yyy or m/d/yy)
290 *
291 * @param string date
292 * @since v1.0
293 */
294 function SetFromAmi($datestring)
295 {
296 $date = explode('/', $datestring);
297 if ( strlen($date[2]) === 2 )
298 {
299 $date[2] = ( ((int) strftime("%C")) * 100) + (int) $date[2];
300 }
301 $this->SetDay($date[1]);
302 $this->SetMonth($date[0]);
303 $this->SetYear($date[2]);
304 }
305
306 /**
307 * Sets date from ISO-Format (YYYY-MM-DD)
308 *
309 * @param string date
310 * @since v1.0
311 */
312 function SetFromIso($datestring)
313 {
314 $date = explode('-', $datestring);
315 if ( strlen($date[2]) === 2 )
316 {
317 $date[2] = ( ((int) strftime("%C")) * 100) + (int) $date[2];
318 }
319 $this->SetDay($date[2]);
320 $this->SetMonth($date[1]);
321 $this->SetYear($date[0]);
322 }
323
324 /**
325 * Sets date to first day of week
326 *
327 * @param int start_of_week 0 = Sunnday, 1 = Monday
328 * @since v1.5
329 */
330 function SetToStartOfWeek($start_of_week = 1)
331 {
332 // get weekday-number
333 $weekday_number = date('w', $this->GetAsTs()) - $start_of_week;
334 if ( $weekday_number === -1 )
335 {
336 // weekday-number is negativ, ($start_of_week was 1 and day is sunday!)
337 $weekday_number = 6;
338 }
339
340 $this->AddDay(-1 * $weekday_number);
341
342 }
343
344 /**
345 * adds given or 1 days to date
346 *
347 * @return bool success
348 * @since v1.3
349 */
350 function AddDay($days = 1)
351 {
352 $ts = mktime(0, 0, 0, $this->GetMonth(), $this->GetDay() + $days, $this->GetYear());
353 $this->Set($ts);
354 return true;
355 }
356
357 /**
358 * checks if date is today
359 *
360 * @return bool true or false
361 * @since v1.15
362 */
363 function IsToday()
364 {
365 if ( $this->GetAsIso() === date('Y-m-d') )
366 {
367 return true;
368 }
369 return false;
370 }
371
372 /**
373 * checks if date is in current month
374 *
375 * @return bool true or false
376 * @since v1.23
377 */
378 function IsCurrentMonth()
379 {
380 if ( $this->GetMonth() == date('m') && $this->GetYear() == date('Y') )
381 {
382 return true;
383 }
384 return false;
385 }
386
387 /**
388 * checks if date is in current month
389 *
390 * @return bool true or false
391 * @since v1.23
392 */
393 function IsCurrentWeek()
394 {
395 if ( $this->GetWeek() == date('W') && $this->GetYear() == date('Y') )
396 {
397 return true;
398 }
399 return false;
400 }
401
402 /**
403 * return true if date is Friday, otherwise false
404 *
405 * @return bool true or false
406 * @since v1.8
407 */
408 function IsFriday()
409 {
410 if ( date('w', $this->GetAsTs()) === '5' )
411 {
412 return true;
413 }
414
415 return false;
416 }
417
418 /**
419 * return true if date is Sunday, otherwise false
420 *
421 * @return bool true or false
422 * @since v1.9
423 */
424 function IsSunday()
425 {
426 if ( date('w', $this->GetAsTs()) === '0' )
427 {
428 return true;
429 }
430
431 return false;
432 }
433
434 /**
435 * return true if date is Sunday, otherwise false
436 *
437 * @return bool true or false
438 * @since v1.9
439 */
440 function IsSaturday()
441 {
442 if ( date('w', $this->GetAsTs()) === '6' )
443 {
444 return true;
445 }
446
447 return false;
448 }
449
450 /**
451 * return true if date is first day in year, otherwise false
452 *
453 * @return bool true or false
454 * @since v1.10
455 */
456 function IsNewYear()
457 {
458 if ( $this->GetDay() === 1 && $this->GetMonth() === 1 )
459 {
460 return true;
461 }
462
463 return false;
464 }
465
466 /**
467 * checks if date is less then given date
468 *
469 * @param mixed date
470 * @return bool true or false
471 * @since v1.17
472 */
473 function IsLT($date = null)
474 {
475 if ( $date === null )
476 {
477 $date = date('Y-m-d');
478 }
479 $date = new Date($date);
480
481 if ( $this->Get() < $date->Get() )
482 {
483 return true;
484 }
485
486 return false;
487 }
488
489 /**
490 * checks if date is less then or equal to given date
491 *
492 * @param mixed date
493 * @return bool true or false
494 * @since v1.17
495 */
496 function IsLE($date = null)
497 {
498 if ( $this->IsLT($date) || $this->IsEq($date) )
499 {
500 return true;
501 }
502
503 return false;
504 }
505
506 /**
507 * checks if date is equal to given date
508 *
509 * @param mixed date
510 * @return bool true or false
511 * @since v1.17
512 */
513 function IsEq($date = null)
514 {
515 if ( $date === null )
516 {
517 $date = date('Y-m-d');
518 }
519 $date = new Date($date);
520
521 if ( $this->Get() === $date->Get() )
522 {
523 return true;
524 }
525
526 return false;
527 }
528
529 /**
530 * checks if date is greater then given date
531 *
532 * @param mixed date
533 * @return bool true or false
534 * @since v1.17
535 */
536 function IsGT($date = null)
537 {
538 if ( $date === null )
539 {
540 $date = date('Y-m-d');
541 }
542 $date = new Date($date);
543
544 if ( $this->Get() > $date->Get() )
545 {
546 return true;
547 }
548
549 return false;
550 }
551
552 /**
553 * checks if date is between date1 and date2
554 *
555 * @param mixed date
556 * @param mixed date
557 * @return bool true or false
558 * @since v1.18
559 */
560 function IsBetween($date1, $date2)
561 {
562 if ( $this->IsGT($date1) && $this->IsLT($date2))
563 {
564 return true;
565 }
566 elseif ( $this->IsGT($date2) && $this->IsLT($date1))
567 {
568 return true;
569 }
570
571 return falsE;
572 }
573
574 }
575
576 function _is_date($date)
577 {
578 if ( preg_match('/[0-9]{4}\-[0-9]{2}\-[0-9]{2}/', $date) )
579 {
580 return true;
581 }
582
583 return false;
584 }
585
586 ?>

Documentation generated on Fri, 26 Sep 2003 15:39:38 +0200 by phpDocumentor 1.2.2