/
ezdate.php
317 lines (279 loc) · 8.26 KB
/
ezdate.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* File containing the eZDate class.
*
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
* @version //autogentag//
* @package lib
*/
/*!
\class eZDate ezdate.php
\ingroup eZLocale
\brief Locale aware date handler
eZDate handles date values in months, days and years.
The time stored as a timestamp with 0 hours, 0 minutes and 0 seconds.
A new instance of eZDate will automaticly use the current locale and current date,
if you however want a different locale use the setLocale() function. The current locale can be
fetched with locale().
Change the date directly with setYear(), setMonth(), setDay() and setMDY().
You can also adjust the date relative to it's current value by using adjustDate().
Use timeStamp() to get the current timestamp value or year(), month() and day()
for the respective values.
When creating new times you're advised to use the static create()
function which returns a new eZDate object. You can also create a copy
with the duplicate() function.
Date checking is done with the isGreaterThan() and isEqualTo() functions.
Text output is done with toString() which can return a long string (default) or
short string representation according to the current locale.
Example:
\code
$us_locale = eZLocale::instance( 'us' );
$date1 = new eZDate();
$date2 = eZDate::create();
$date2->setLocale( $us_locale );
$date2->adjustDate( 1, 2, 3 );
$date3 = $date1->duplicate();
print( $date1->toString() );
print( $date2->toString( true ) );
print( $date1->isEqualTo( $date3 ) ? 'true' : 'false' ); // Prints 'true'
\endcode
\sa eZTime, eZDateTime, eZLocale
*/
class eZDate
{
/*!
Creates a new date object with default locale, if $date is not supplied
the current date is used.
*/
function eZDate( $date = false )
{
if ( $date === false )
{
$date = mktime( 0, 0, 0 );
}
else
{
$arr = getdate( $date );
$date = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] );
}
$this->Date = $date;
$this->Locale = eZLocale::instance();
$this->IsValid = true;
}
function attributes()
{
return array( 'timestamp',
'is_valid',
'year',
'month',
'day' );
}
function hasAttribute( $name )
{
return in_array( $name, $this->attributes() );
}
function attribute( $name )
{
if ( $name == 'timestamp' )
return $this->timeStamp();
else if ( $name == 'is_valid' )
return $this->isValid();
else if ( $name == 'day' )
return $this->day();
else if ( $name == 'year' )
return $this->year();
else if ( $name == 'month' )
return $this->month();
eZDebug::writeError( "Attribute '$name' does not exist", __METHOD__ );
return false;
}
/*!
\return true if the date has valid data.
*/
function isValid()
{
return $this->IsValid;
}
/*!
Sets the locale to $locale which is used in text output.
*/
function setLocale( $locale )
{
$this->Locale = $locale;
}
/*!
Returns the current locale.
*/
function locale()
{
return $this->Locale;
}
/*!
Returns the timestamp value, this is the number of seconds since the epoch
with hours, minutes and seconds set to 0.
\note The value is returned as a reference and should not be modified.
*/
function timeStamp()
{
return $this->Date;
}
function setTimeStamp( $stamp )
{
$this->Date = $stamp;
$this->IsValid = true;
}
/*!
Sets the year leaving the other elements untouched.
*/
function setYear( $year )
{
$arr = getdate( $this->Date );
$this->Date = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $year );
}
/*!
Sets the month leaving the other elements untouched.
*/
function setMonth( $month )
{
$arr = getdate( $this->Date );
$this->Date = mktime( 0, 0, 0, $month, $arr['mday'], $arr['year'] );
}
/*!
Sets the day leaving the other elements untouched.
*/
function setDay( $day )
{
$arr = getdate( $this->Date );
$this->Date = mktime( 0, 0, 0, $arr['mon'], $day, $arr['year'] );
}
/*!
Returns the year element.
*/
function year()
{
return date( 'Y', $this->Date );
}
/*!
Returns the month element.
*/
function month()
{
return date( 'm', $this->Date );
}
/*!
Returns the day element.
*/
function day()
{
return date( 'd', $this->Date );
}
/*!
Sets the year, month and day elements. If $day or $year is omitted or set 0
they will get a value taken from the current time.
*/
function setMDY( $month, $day = 0, $year = 0 )
{
if ( $year != 0 )
$date = mktime( 0, 0, 0, $month, $day, $year );
else if ( $day != 0 )
$date = mktime( 0, 0, 0, $month, $day );
else
$date = mktime( 0, 0, 0, $month );
$this->Date = $date;
}
/*!
Adjusts the date relative to it's current value. This is useful for adding/subtracting
years, months or days to an existing date.
*/
function adjustDate( $month, $day = 0, $year = 0 )
{
$arr = getdate( $this->Date );
$date = mktime( 0, 0, 0, $month + $arr['mon'], $day + $arr['mday'], $year + $arr['year'] );
$this->Date = $date;
}
/*!
Returns true if this object has a date greater than $date. $date can be specified as
a timestamp value or as an eZDate object. If $equal is true it returns true if
they are equal as well.
*/
function isGreaterThan( $date, $equal = false )
{
$d1 = $this->timeStamp();
if ( $date instanceof eZDate )
{
$d2 = $date->timeStamp();
}
else
{
$arr = getdate( $date );
$d2 = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] );
}
if ( $d1 > $d2 )
return true;
else if ( $equal and $d1 == $d2 )
return true;
else
return false;
}
/*!
Returns true if this object is equal to $date. $date can be specified as
a timestamp value or as an eZDate object.
*/
function isEqualTo( $date )
{
$d1 = $this->timeStamp();
if ( $date instanceof eZDate )
{
$d2 = $date->timeStamp();
}
else
{
$arr = getdate( $date );
$d2 = mktime( 0, 0, 0, $arr['mon'], $arr['mday'], $arr['year'] );
}
return $d1 == $d2;
}
/*!
Creates a new eZDate object with the date values $month, $day and $year and returns a reference to it.
Any value can be ommitted or set to 0 to use the current date value.
*/
static function create( $month, $day = 0, $year = 0 )
{
if ( $year != 0 )
$date = mktime( 0, 0, 0, $month, $day, $year );
else if ( $day != 0 )
$date = mktime( 0, 0, 0, $month, $day );
else
$date = mktime( 0, 0, 0, $month );
$newDateObject = new eZDate( $date );
return $newDateObject;
}
/*!
\deprecated This function is deprecated in PHP5, use the PHP5 clone keyword instead
Creates an exact copy of this object and returns it.
*/
function duplicate()
{
$copy = clone $this;
return $copy;
}
/*!
Creates a string representation of the date using the current locale and returns it.
If $short is true a short representation is used.
*/
function toString( $short = false )
{
if ( $short )
$str = $this->Locale->formatShortDate( $this->Date );
else
$str = $this->Locale->formatDate( $this->Date );
return $str;
}
/// Locale object, is just a reference to minimize memory usage.
public $Locale;
/// The current date as a timestamp without hour, minute or second values
public $Date;
public $IsValid;
}
?>