1: <?php
2: /**
3: * Copyright 2009-2012 Horde LLC (http://www.horde.org/)
4: *
5: * See the enclosed file COPYING for license information (LGPL). If you
6: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
7: *
8: * @category Horde
9: * @package Date
10: */
11:
12: /**
13: * A Span represents a range of time.
14: *
15: * @author Chuck Hagenbuch <chuck@horde.org>
16: * @category Horde
17: * @package Date
18: */
19: class Horde_Date_Span
20: {
21: /**
22: * @var Horde_Date
23: */
24: public $begin;
25:
26: /**
27: * @var Horde_Date
28: */
29: public $end;
30:
31: /**
32: * Constructor.
33: *
34: * @param mixed $begin Horde_Date or other format accepted by the
35: * Horde_Date constructor.
36: * @param mixed $end Horde_Date or other format accepted by the
37: * Horde_Date constructor.
38: */
39: public function __construct($begin, $end)
40: {
41: if (!($begin instanceof Horde_Date)) {
42: $begin = new Horde_Date($begin);
43: }
44: if (!($end instanceof Horde_Date)) {
45: $end = new Horde_Date($end);
46: }
47:
48: $this->begin = $begin;
49: $this->end = $end;
50: }
51:
52: /**
53: * Returns the width of this span in seconds.
54: */
55: public function width()
56: {
57: return abs($this->end->timestamp() - $this->begin->timestamp());
58: }
59:
60: /**
61: * Is a Horde_Date within this span?
62: *
63: * @param Horde_Date $date
64: */
65: public function includes($date)
66: {
67: return ($this->begin->compareDateTime($date) <= 0) && ($this->end->compareDateTime($date) >= 0);
68: }
69:
70: /**
71: * Add a number of seconds to this span, returning the new span
72: */
73: public function add($factor)
74: {
75: return new Horde_Date_Span($this->begin->add($factor), $this->end->add($factor));
76: }
77:
78: /**
79: * Subtract a number of seconds from this span, returning the new span.
80: */
81: public function sub($factor)
82: {
83: return new Horde_Date_Span($this->begin->sub($factor), $this->end->sub($factor));
84: }
85:
86: public function __toString()
87: {
88: return '(' . $this->begin . '..' . $this->end . ')';
89: }
90:
91: }
92: