-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateTime.php
198 lines (171 loc) · 3.7 KB
/
DateTime.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
/**
* --------------------------------
* PEL - Phalcon Extensions Library
* --------------------------------
*
* This code is distributed under New BSD license.
* License is bundled with this package in file LICENSE.txt.
*
* @author Jiri Pazdernik <[email protected]>
*/
namespace Pel;
/**
* Class extending default PHP DateTime class
*
* @author Jiri Pazdernik <[email protected]>
*/
class DateTime extends \DateTime
{
/**
* Default date format
*
* @var string
*/
const DEFAULT_DATE_FORMAT = "Y-m-d";
/**
* Default time format
*
* @var string
*/
const DEFAULT_TIME_FORMAT = "H:i:s";
/**
* Default format for self::format() method
*
* @var string
*/
protected $_defaultFormat = self::ATOM;
/**
* Add days to date
*
* @param number $days number of days to add
* @return \Pel\DateTime
*/
public function addDay($days = 1)
{
$days = (int) $days;
$this->add(new \DateInterval("P{$days}D"));
return $this;
}
/**
* Returns new DateTime object formatted according to the specified format
*
* @param string $format format string
* @param string $time date and time value
* @param object $timezone
* @return \Pel\DateTime
*/
public static function createFromFormat($format, $time, $timezone = null)
{
$dt = new static();
if (null === $timezone) {
$parent = parent::createFromFormat($format, $time);
} else {
$parent = parent::createFromFormat($format, $time, $timezone);
}
if (false === $parent) {
return false;
}
$dt->setDefaultFormat($format);
$dt->setTimestamp($parent->getTimestamp());
return $dt;
}
/**
* Returns date formatted according to given format
*
* @param string $format (OPTIONAL) format parameters
* @return string
*/
public function format($format = null)
{
if (null === $format) {
$format = $this->_defaultFormat;
}
return parent::format($format);
}
/**
* Returns date part formatted according to given format
*
* @param string $format (OPTIONAL) format parameters
* @return string
*/
public function getDate($format = null)
{
if (null === $format) {
$format = self::DEFAULT_DATE_FORMAT;
}
return $this->format($format);
}
/**
* Get default format
*
* @return string
*/
public function getDefaultFormat()
{
return $this->_defaultFormat;
}
/**
* Returns time part formatted according to given format
*
* @param string $format (OPTIONAL) format parameters
* @return string
*/
public function getTime($format = null)
{
if (null === $format) {
$format = self::DEFAULT_TIME_FORMAT;
}
return $this->format($format);
}
/**
* Check if date is weekend
*
* @return boolean
*/
public function isWeekend()
{
return $this->format("N") > 5;
}
/**
* Set new date and/or time formatted according to the specified format
*
* @param string $format format string
* @param string $time date and time value
* @return \Pel\DateTime
*/
public function setFromFormat($format, $time)
{
$parent = parent::createFromFormat($format, $time);
if (false !== $parent) {
$this->setTimestamp($parent->getTimestamp());
}
return $dt;
}
/**
* Set new date and/or time formatted according to the default format
*
* @param string $time date and time value
* @return \Pel\DateTime
*/
public function set($time)
{
$format = $this->_defaultFormat;
$parent = parent::createFromFormat($format, $time);
if (false !== $parent) {
$this->setTimestamp($parent->getTimestamp());
}
return $this;
}
/**
* Set default format
*
* @param string $format
* @return \Pel\DateTime
*/
public function setDefaultFormat($format)
{
$this->_defaultFormat = $format;
return $this;
}
}