PageRenderTime 17ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 2ms app.codeStats 0ms

/htdocs/system/classes/habaridatetime.php

https://github.com/psaintlaurent/Habari
PHP | 353 lines | 158 code | 39 blank | 156 comment | 10 complexity | 2ebe65b9be81a277d057bb099fecb3c2 MD5 | raw file
  1<?php
  2/**
  3 * @package Habari
  4 *
  5 */
  6
  7/**
  8 * HabariDateTime class to wrap dates in.
  9 *
 10 * @property-read HabariDateTime $clone Returns a clonned object.
 11 * @property-read string $sql Returns a unix timestamp for inserting into DB.
 12 * @property-read int $int Returns a unix timestamp as integer.
 13 */
 14class HabariDateTime extends DateTime
 15{
 16	private static $default_timezone;
 17	private static $default_datetime_format = 'c';
 18	private static $default_date_format;
 19	private static $default_time_format;
 20
 21	/**
 22	 * Set default timezone to system default on init.
 23	 *
 24	 * @static
 25	 */
 26	public static function __static()
 27	{
 28
 29		if ( Options::get( 'timezone' ) ) {
 30			self::set_default_timezone( Options::get( 'timezone' ) );
 31		}
 32
 33		self::$default_timezone = date_default_timezone_get();
 34
 35		self::$default_date_format = Options::get('dateformat');
 36		self::$default_time_format = Options::get('timeformat');
 37
 38		if ( self::$default_date_format || self::$default_time_format ) {
 39			self::set_default_datetime_format( self::$default_date_format . ' ' . self::$default_time_format );
 40		}
 41	}
 42
 43	/**
 44	 * Set default date/time format. The format is the same as the
 45	 * internal php {@link http://ca.php.net/date date() function}.
 46	 *
 47	 * @static
 48	 * @param string $format The date format.
 49	 */
 50	public static function set_default_datetime_format( $format )
 51	{
 52		self::$default_datetime_format = $format;
 53	}
 54
 55	/**
 56	 * Get the default date/time format set.
 57	 *
 58	 * @static
 59	 * @see set_default_datetime_format()
 60	 * @return string The date format set.
 61	 */
 62	public static function get_default_datetime_format()
 63	{
 64		return self::$default_datetime_format;
 65	}
 66
 67	/**
 68	 * Sets the timezone for Habari and PHP.
 69	 *
 70	 * @static
 71	 * @param string $timezone A timezone name, not an abbreviation, for example 'America/New York'
 72	 */
 73	public static function set_default_timezone( $timezone )
 74	{
 75		self::$default_timezone = $timezone;
 76		date_default_timezone_set( self::$default_timezone );
 77	}
 78
 79	/**
 80	 * Get the timezone for Habari and PHP.
 81	 * Defaults to system timezone if not set.
 82	 *
 83	 * @static
 84	 * @see set_default_timezone()
 85	 * @param string The deafult timezone.
 86	 */
 87	public static function get_default_timezone()
 88	{
 89		return self::$default_timezone;
 90	}
 91
 92	/**
 93	 * Helper function to create a HabariDateTime object for the given
 94	 * time and timezone. If no time is given, defaults to 'now'. If no
 95	 * timezone given defaults to timezone set in {@link set_default_timezone()}
 96	 *
 97	 * @static
 98	 * @see DateTime::__construct()
 99	 * @param string $time String in a format accepted by
100	 * {@link http://ca.php.net/strtotime strtotime()}, defaults to "now".
101	 * @param string $timezone A timezone name, not an abbreviation.
102	 */
103	public static function date_create( $time = null, $timezone = null )
104	{
105		if ( $time instanceOf HabariDateTime ) {
106			return $time;
107		}
108		elseif ( $time instanceOf DateTime ) {
109			$time = $time->format('U');
110		}
111		elseif ( $time == null ) {
112			$time = 'now';
113		}
114		elseif ( is_numeric($time) ) {
115			$time = '@' . $time;
116		}
117
118		if ( $timezone === null ) {
119			$timezone = self::$default_timezone;
120		}
121
122		// passing the timezone to construct doesn't seem to do anything.
123		$datetime = new HabariDateTime($time);
124		$datetime->set_timezone($timezone);
125		return $datetime;
126	}
127
128	/**
129	 * Set the date of this object
130	 *
131	 * @see DateTime::setDate()
132	 * @param int $year Year of the date
133	 * @param int $month Month of the date
134	 * @param int $day Day of the date
135	 */
136	public function set_date( $year, $month, $day )
137	{
138		parent::setDate($year, $month, $day);
139		return $this;
140	}
141
142	/**
143	 * Sets the ISO date
144	 *
145	 * @see DateTime::setISODate()
146	 * @param int $year Year of the date
147	 * @param int $month Month of the date
148	 * @param int $day Day of the date
149	 */
150	public function set_isodate( $year, $week, $day = null )
151	{
152		parent::setISODate($year, $week, $day);
153		return $this;
154	}
155
156	/**
157	 * Set the time of this object
158	 *
159	 * @see DateTime::setTime()
160	 * @param int $hour Hour of the time
161	 * @param int $minute Minute of the time
162	 * @param int $second Second of the time
163	 */
164	public function set_time( $hour, $minute, $second = null )
165	{
166		parent::setTime($hour, $minute, $second);
167		return $this;
168	}
169
170	/**
171	 * Set the timezone for this datetime object. Can be either string
172	 * timezone identifier, or DateTimeZone object.
173	 *
174	 * @see DateTime::setTimezone()
175	 * @param mixed The timezone to use.
176	 * @return HabariDateTime $this object.
177	 */
178	public function set_timezone( $timezone )
179	{
180		if ( ! $timezone instanceof DateTimeZone ) {
181			$timezone = new DateTimeZone($timezone);
182		}
183		parent::setTimezone($timezone);
184		return $this;
185	}
186
187	/**
188	 * Get the timezone identifier that is set for this datetime object.
189	 *
190	 * @return DateTimeZone The timezone object.
191	 */
192	public function get_timezone()
193	{
194		return parent::getTimezone();
195	}
196
197	/**
198	 * Returns date formatted according to given format.
199	 *
200	 * @see DateTime::format()
201	 * @param string $format Format accepted by {@link http://php.net/date date()}.
202	 * @return string The formatted date, false on failure.
203	 */
204	public function format($format = null)
205	{
206		if ( $format === null ) {
207			$format = self::$default_datetime_format;
208		}
209		return parent::format($format);
210	}
211
212	/**
213	 * Returns date components inserted into a string
214	 * 
215	 * Example:
216	 * echo HabariDateTime::date_create('2010-01-01')->text_format('The year was {Y}.');
217	 * // Expected output:  The year was 2010.	 	  	
218	 *	
219	 * @param string $format A string with single-character date format codes {@link http://php.net/date date()} surrounded by braces
220	 * @return string The string with date components inserted	 
221	 */	 
222	public function text_format($format)
223	{
224		return preg_replace_callback('%\{(\w)\}%iu', array($this, 'text_format_callback'), $format);
225	}
226
227	/**
228	 * Callback method for supplying replacements for HabariDatTime::text_format()
229	 * 
230	 * @param array $matches The matches found in the regular expression.
231	 * @return string The date component value for the matched character.
232	 */	 
233	private function text_format_callback($matches)
234	{
235		return $this->format($matches[1]);
236	}
237
238	/**
239	 * Alters the timestamp
240	 *
241	 * @param string $format A format accepted by {@link http://php.net/strtotime strtotime()}.
242	 * @return HabariDateTime $this object.
243	 */
244	public function modify( $args )
245	{
246		parent::modify( $args );
247		return $this;
248	}
249
250	/**
251	 * @see format()
252	 */
253	public function get($format = null)
254	{
255		return $this->format($format);
256	}
257
258	/**
259	 * Echos date formatted according to given format.
260	 *
261	 * @see format()
262	 * @param string $format Format accepted by {@link http://php.net/date date()}.
263	 */
264	public function out($format = null)
265	{
266		echo $this->format($format);
267	}
268
269	/**
270	 * Magic method called when this object is cast to string. Returns the
271	 * unix timestamp of this object.
272	 *
273	 * @return string The unix timestamp
274	 */
275	public function __toString()
276	{
277		return $this->format('U');
278	}
279
280	/**
281	 * Magic method to get magic ponies... properties, I mean.
282	 */
283	public function __get($property)
284	{
285		switch ($property) {
286			case 'clone':
287				return clone $this;
288
289			case 'sql':
290				return $this->format('U');
291				break;
292
293			case 'int':
294				return intval( $this->format('U') );
295				break;
296
297			case 'time':
298				return $this->format( self::get_default_time_format() );
299				break;
300
301			case 'date':
302				return $this->format( self::get_default_date_format() );
303				break;
304
305			default:
306				$info = getdate($this->format('U'));
307				$info['mon0'] = substr('0' . $info['mon'], -2, 2);
308				$info['mday0'] = substr('0' . $info['mday'], -2, 2);
309				if ( isset($info[$property]) ) {
310					return $info[$property];
311				}
312				return $this->$property;
313		}
314	}
315
316	/**
317	 * Return the default date format, as set in the Options table
318	 *
319	 * @return The default date format
320	 **/
321	public static function get_default_date_format ( ) {
322
323		return self::$default_date_format;
324
325	}
326
327	/**
328	 * Return the default time format, as set in the Options table
329	 *
330	 * @return The default time format
331	 **/
332	public static function get_default_time_format ( ) {
333
334		return self::$default_time_format;
335
336	}
337
338	/**
339	 * Returns an associative array containing the date information for
340	 * this HabariDateTime object, as per {@link http://php.net/getdate getdate()}
341	 *
342	 * @return array Associative array containing the date information
343	 */
344	public function getdate()
345	{
346		$info = getdate($this->format('U'));
347		$info['mon0'] = substr('0' . $info['mon'], -2, 2);
348		$info['mday0'] = substr('0' . $info['mday'], -2, 2);
349		return $info;
350	}
351}
352
353?>