2 // +----------------------------------------------------------------------+
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 The PHP Group |
6 // +----------------------------------------------------------------------+
7 // | This source file is subject to version 2.0 of the PHP license, |
8 // | that is bundled with this package in the file LICENSE, and is |
9 // | available at through the world-wide-web at |
10 // | http://www.php.net/license/2_02.txt. |
11 // | If you did not receive a copy of the PHP license and are unable to |
12 // | obtain it through the world-wide-web, please send a note to |
13 // | license@php.net so we can mail you a copy immediately. |
14 // +----------------------------------------------------------------------+
15 // | Authors: Wolfram Kriesing <wk@visionp.de> |
17 // +----------------------------------------------------------------------+//
18 // $Id: DateTime.php 136857 2003-08-07 07:46:23Z cain $
20 require_once 'I18N/Format.php';
22 define( 'I18N_DATETIME_SHORT' , 1 );
23 define( 'I18N_DATETIME_DEFAULT' , 2 );
24 define( 'I18N_DATETIME_MEDIUM' , 3 );
25 define( 'I18N_DATETIME_LONG' , 4 );
26 define( 'I18N_DATETIME_FULL' , 5 );
35 class I18N_DateTime extends I18N_Format
46 const CUSTOM_FORMATS_OFFSET = 100;
49 var $days = array( 'Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' );
51 var $daysAbbreviated = array( 'Sun','Mon','Tue','Wed','Thu','Fri','Sat');
53 var $monthsAbbreviated = array( 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' ,'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' );
71 * this var contains the current locale this instace works with
74 * @var string this is a string like 'de_DE' or 'en_US', etc.
79 * the locale object which contains all the formatting specs
84 var $_localeObj = null;
87 var $_currentFormat = I18N_DATETIME_DEFAULT;
88 var $_currentDateFormat = I18N_DATETIME_DEFAULT;
89 var $_currentTimeFormat = I18N_DATETIME_DEFAULT;
91 var $_customFormats = array();
94 * Use this method to setup and to retreive the static instance of the I18N_DateTime.
96 * // setup the object with the proper locale
97 * I18N_DateTime::singleton($locale);
98 * // and anywhere in your code you can call the following
99 * // and you get the instance for this very locale, you specified via singleton()
100 * $dateTime = I18N_DateTime::singleton();
105 * @param string the locale to use, i.e. 'de_DE'
106 * @return object an instance of this class
108 function &singleton($locale=null)
110 return I18N_Format::_singleton($locale,__CLASS__);
114 * returns the timestamp formatted according to the locale and the format-mode
115 * use this method to format a date and time timestamp
119 * @author Wolfram Kriesing <wolfram@kriesing.de>
120 * @param int a timestamp
121 * @param int the formatting mode, using setFormat you can add custom formats
122 * @return string the formatted timestamp
125 function format($timestamp=null,$format=null)
127 return $this->_format($timestamp,$format);
131 * returns the timestamp formatted according to the locale and the format-mode
132 * use this method to get a formatted date only
134 * @see setDateFormat()
136 * @author Wolfram Kriesing <wolfram@kriesing.de>
137 * @param int a timestamp
138 * @param int the formatting mode, use setDateFormat to add custom formats
139 * @return string the formatted timestamp
142 function formatDate( $timestamp=null , $format=null )
144 return $this->_formatDateTime($timestamp,$format,'date');
148 * returns the timestamp formatted according to the locale and the format-mode
149 * use this method to get a formatted time only
151 * @see setTimeFormat()
153 * @author Wolfram Kriesing <wolfram@kriesing.de>
154 * @param int a timestamp
155 * @param int the formatting mode, use setTimeFormat to add custom formats
156 * @return string the formatted timestamp
159 function formatTime( $timestamp=null , $format=null )
161 return $this->_formatDateTime($timestamp,$format,'time');
165 * formats a timestamp consisting of date and time
166 * or a custom timestamp, which was set using setFormat
170 * @author Wolfram Kriesing <wolfram@kriesing.de>
171 * @param int a timestamp
172 * @param int the format
173 * @return string the formatted timestamp
176 function _format( $timestamp , $format )
178 if ($format == null){
179 $format = $this->getFormat();
181 if ($timestamp == null){
185 if ($format >= I18N_CUSTOM_FORMATS_OFFSET) {
186 if (isset($this->_customFormats[$format])) {
187 return $this->_translate(date($this->_customFormats[$format],$timestamp));
189 $format = I18N_DATETIME_DEFAULT;
192 return $this->_formatDateTime($timestamp,$format,'date').' '.
193 $this->_formatDateTime($timestamp,$format,'time');
197 * this method formats the given timestamp into the given format
200 * @see setDateFormat()
201 * @see setTimeFormat()
203 * @author Wolfram Kriesing <wolfram@kriesing.de>
204 * @param int a timestamp
205 * @param int the formatting mode, use setTimeFormat to add custom formats
206 * @param string either 'date' or 'time' so this method knows what it is currently formatting
207 * @return string the formatted timestamp
210 function _formatDateTime($timestamp,$format,$what)
212 $getFormatMethod = 'get'.ucfirst($what).'Format';
213 if ($format == null){
214 $format = $this->$getFormatMethod();
216 if ($timestamp == null){
220 $curFormat = I18N_DATETIME_DEFAULT;// just in case the if's below dont find nothing
221 $formatArray = $what.'Formats';
222 if (isset($this->_localeObj->{$formatArray}[$format])) {
223 $curFormat = $this->_localeObj->{$formatArray}[$format];
224 } elseif(isset($this->_customFormats[$format])) {
225 $curFormat = $this->_customFormats[$format];
227 return $this->_translate(date($curFormat,$timestamp));
231 * this simply translates the formatted timestamp into the locale-language
234 * @author Wolfram Kriesing <wolfram@kriesing.de>
235 * @param string a human readable timestamp, such as 'Monday, August 7 2002'
236 * @return string the formatted timestamp
239 function _translate( $string )
241 //FIXXME optimize this array, use only those that are in the format string, i.e. if no abbreviated formats are used
242 // dont put the abbreviated's in this array ....
243 $translateArrays = array('days','months','daysAbbreviated','monthsAbbreviated');
245 // this seems a bit difficult i guess,
246 // but i had problems with the way i did it before, which way simply str_replace the
247 // src-lang array and the dest-lang array, this caused stuff like
248 // translating 'Monday' => 'Montag' and then 'Montag' => 'Motag', since 'Mon'
249 // is the abbreviated weekday for Monday.
250 // if i would turn it around and translate the abbreviated words first it would screw up worse
252 // so what i do now is searching for the position of words which can be translated and
253 // remember the position (using strpos) and dont translate a word at this position a second
254 // time. this at least prevents the case described above. i hope it covers everything else too
255 // for me it works quite well now
256 $translateSrc = array();
257 $translateDest = array();
258 $prevPositions = array();
259 foreach ($translateArrays as $aArray) {
260 if (isset($this->_localeObj->{$aArray})) {
261 foreach ($this->{$aArray} as $index=>$aWord) {
262 if (($pos=strpos($string,$aWord))!==false && !in_array($pos,$prevPositions)) {
263 $translateSrc[] = $aWord;
264 $translateDest[] = $this->_localeObj->{$aArray}[$index];
265 $prevPositions[] = $pos;
270 // here we actually replace the strings (translate:-)) that we found, when checking for their position
271 $string = str_replace($translateSrc,$translateDest,$string);
276 * define a custom format given by $format and return the $format-id
277 * the format-id can be used to call format( x , format-id ) to
278 * tell the method you want to use the format with that id
282 * @author Wolfram Kriesing <wolfram@kriesing.de>
283 * @param string defines a custom format
284 * @return int the format-id, to be used with the format-method
286 function setFormat( $format=I18N_DATETIME_DEFAULT )
288 return parent::setFormat( $format );
292 * define a custom format given by $format and return the $format-id
293 * the format-id can be used to call formatDate( x , format-id ) to
294 * tell the method you want to use the format with that id
298 * @author Wolfram Kriesing <wolfram@kriesing.de>
299 * @param string defines a custom format
300 * @return int the format-id, to be used with the format-method
302 function setDateFormat( $format=I18N_DATETIME_DEFAULT )
304 return $this->_setFormat( $format , 'date' );
308 * define a custom format given by $format and return the $format-id
309 * the format-id can be used to call formatTime( x , format-id ) to
310 * tell the method you want to use the format with that id
314 * @author Wolfram Kriesing <wolfram@kriesing.de>
315 * @param string defines a custom format
316 * @return int the format-id, to be used with the format-method
318 function setTimeFormat( $format=I18N_DATETIME_DEFAULT )
320 return $this->_setFormat( $format , 'time' );
326 function getDateFormat()
328 return $this->_currentDateFormat;
330 function getTimeFormat()
332 return $this->_currentTimeFormat;
336 * get either the current or the given month name
339 function getMonthName( $which=null , $abbreviated=false )
342 $which = $date('n')-1;
344 $monthNames = $this->getMonthNames( $abbreviated );
345 return $monthNames[$which];
349 * get all month names for the current locale
351 * get all month names for the current locale,
352 * fallback to english if not defined
355 function getMonthNames($abbreviated=false)
357 $propName = 'months'.($abbreviated ? 'Abbreviated' : '' );
358 return isset($this->_localeObj->$propName) ? $this->_localeObj->$propName : $this->$propName;
361 function getDayNames($abbreviated=false)
363 $propName = 'days'.($abbreviated ? 'Abbreviated' : '' );
364 return isset($this->_localeObj->$propName) ? $this->_localeObj->$propName : $this->$propName;
368 // all the following are simply convienence methods
369 // which make it shorter to call the format methods with the default
371 // FIXXME we should use overloading here, well with ZE2 we will!!!!
373 // i am not really happy with the following, since it only bloats the code,
374 // but the methods make sense :-)
378 * convinience method, same as format( $timestamp , I18N_DATETIME_SHORT )
380 * this method exists, to have a shorter call to the method
381 * with a default format I18N_DATETIME_SHORT
385 * @author Wolfram Kriesing <wolfram@kriesing.de>
386 * @param int a timestamp
387 * @return string the formatted timestamp
390 function formatShort( $timestamp=null )
392 return $this->format( $timestamp , I18N_DATETIME_SHORT );
396 * convinience method, same as format( $timestamp , I18N_DATETIME_DEFAULT )
398 * this method exists, to have a shorter call to the method
399 * with a default format I18N_DATETIME_DEFAULT
403 * @author Wolfram Kriesing <wolfram@kriesing.de>
404 * @param int a timestamp
405 * @return string the formatted timestamp
408 function formatDefault( $timestamp=null )
410 return $this->format( $timestamp , I18N_DATETIME_DEFAULT );
414 * convinience method, same as format( $timestamp , I18N_DATETIME_MEDIUM )
416 * this method exists, to have a shorter call to the method
417 * with a default format I18N_DATETIME_MEDIUM
421 * @author Wolfram Kriesing <wolfram@kriesing.de>
422 * @param int a timestamp
423 * @return string the formatted timestamp
426 function formatMedium( $timestamp=null )
428 return $this->format( $timestamp , I18N_DATETIME_MEDIUM );
432 * convinience method, same as format( $timestamp , I18N_DATETIME_LONG )
434 * this method exists, to have a shorter call to the method
435 * with a default format I18N_DATETIME_LONG
439 * @author Wolfram Kriesing <wolfram@kriesing.de>
440 * @param int a timestamp
441 * @return string the formatted timestamp
444 function formatLong( $timestamp=null )
446 return $this->format( $timestamp , I18N_DATETIME_LONG );
450 * convinience method, same as format( $timestamp , I18N_DATETIME_FULL )
452 * this method exists, to have a shorter call to the method
453 * with a default format I18N_DATETIME_FULL
457 * @author Wolfram Kriesing <wolfram@kriesing.de>
458 * @param int a timestamp
459 * @return string the formatted timestamp
462 function formatFull( $timestamp=null )
464 return $this->format( $timestamp , I18N_DATETIME_FULL );
471 * convinience method, same as formatDate( $timestamp , I18N_DATETIME_SHORT )
473 * this method exists, to have a shorter call to the method
474 * with a default format I18N_DATETIME_SHORT
478 * @author Wolfram Kriesing <wolfram@kriesing.de>
479 * @param int a timestamp
480 * @return string the formatted timestamp
483 function formatDateShort( $timestamp=null )
485 return $this->formatDate( $timestamp , I18N_DATETIME_SHORT );
489 * convinience method, same as formatDate( $timestamp , I18N_DATETIME_DEFAULT )
491 * this method exists, to have a shorter call to the method
492 * with a default format I18N_DATETIME_DEFAULT
496 * @author Wolfram Kriesing <wolfram@kriesing.de>
497 * @param int a timestamp
498 * @return string the formatted timestamp
501 function formatDateDefault( $timestamp=null )
503 return $this->formatDate( $timestamp , I18N_DATETIME_DEFAULT );
507 * convinience method, same as formatDate( $timestamp , I18N_DATETIME_MEDIUM )
509 * this method exists, to have a shorter call to the method
510 * with a default format I18N_DATETIME_MEDIUM
514 * @author Wolfram Kriesing <wolfram@kriesing.de>
515 * @param int a timestamp
516 * @return string the formatted timestamp
519 function formatDateMedium( $timestamp=null )
521 return $this->formatDate( $timestamp , I18N_DATETIME_MEDIUM );
525 * convinience method, same as formatDate( $timestamp , I18N_DATETIME_LONG )
527 * this method exists, to have a shorter call to the method
528 * with a default format I18N_DATETIME_LONG
532 * @author Wolfram Kriesing <wolfram@kriesing.de>
533 * @param int a timestamp
534 * @return string the formatted timestamp
537 function formatDateLong( $timestamp=null )
539 return $this->formatDate( $timestamp , I18N_DATETIME_LONG );
543 * convinience method, same as formatDate( $timestamp , I18N_DATETIME_FULL )
545 * this method exists, to have a shorter call to the method
546 * with a default format I18N_DATETIME_FULL
550 * @author Wolfram Kriesing <wolfram@kriesing.de>
551 * @param int a timestamp
552 * @return string the formatted timestamp
555 function formatDateFull( $timestamp=null )
557 return $this->formatDate( $timestamp , I18N_DATETIME_FULL );
565 * convinience method, same as formatTime( $timestamp , I18N_DATETIME_SHORT )
567 * this method exists, to have a shorter call to the method
568 * with a default format I18N_DATETIME_SHORT
572 * @author Wolfram Kriesing <wolfram@kriesing.de>
573 * @param int a timestamp
574 * @return string the formatted timestamp
577 function formatTimeShort( $timestamp=null )
579 return $this->formatTime( $timestamp , I18N_DATETIME_SHORT );
583 * convinience method, same as formatTime( $timestamp , I18N_DATETIME_DEFAULT )
585 * this method exists, to have a shorter call to the method
586 * with a default format I18N_DATETIME_DEFAULT
590 * @author Wolfram Kriesing <wolfram@kriesing.de>
591 * @param int a timestamp
592 * @return string the formatted timestamp
595 function formatTimeDefault( $timestamp=null )
597 return $this->formatTime( $timestamp , I18N_DATETIME_DEFAULT );
601 * convinience method, same as formatTime( $timestamp , I18N_DATETIME_MEDIUM )
603 * this method exists, to have a shorter call to the method
604 * with a default format I18N_DATETIME_MEDIUM
608 * @author Wolfram Kriesing <wolfram@kriesing.de>
609 * @param int a timestamp
610 * @return string the formatted timestamp
613 function formatTimeMedium( $timestamp=null )
615 return $this->formatTime( $timestamp , I18N_DATETIME_MEDIUM );
619 * convinience method, same as formatTime( $timestamp , I18N_DATETIME_LONG )
621 * this method exists, to have a shorter call to the method
622 * with a default format I18N_DATETIME_LONG
626 * @author Wolfram Kriesing <wolfram@kriesing.de>
627 * @param int a timestamp
628 * @return string the formatted timestamp
631 function formatTimeLong( $timestamp=null )
633 return $this->formatTime( $timestamp , I18N_DATETIME_LONG );
637 * convinience method, same as formatTime( $timestamp , I18N_DATETIME_FULL )
639 * this method exists, to have a shorter call to the method
640 * with a default format I18N_DATETIME_FULL
644 * @author Wolfram Kriesing <wolfram@kriesing.de>
645 * @param int a timestamp
646 * @return string the formatted timestamp
649 function formatTimeFull( $timestamp=null )
651 return $this->formatTime( $timestamp , I18N_DATETIME_FULL );