Source for file DateTime.php
Documentation is available at DateTime.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002, 2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Wolfram Kriesing <wk@visionp.de> |
// +----------------------------------------------------------------------+//
// $Id: DateTime.php 136857 2003-08-07 07:46:23Z cain $
require_once 'I18N/Format.php';
define( 'I18N_DATETIME_SHORT' , 1 );
define( 'I18N_DATETIME_DEFAULT' , 2 );
define( 'I18N_DATETIME_MEDIUM' , 3 );
define( 'I18N_DATETIME_LONG' , 4 );
define( 'I18N_DATETIME_FULL' , 5 );
const CUSTOM_FORMATS_OFFSET = 100;
var $days = array ( 'Sunday' , 'Monday' , 'Tuesday' , 'Wednesday' , 'Thursday' , 'Friday' , 'Saturday' );
var $monthsAbbreviated = array ( 'Jan' , 'Feb' , 'Mar' , 'Apr' , 'May' , 'Jun' ,'Jul' , 'Aug' , 'Sep' , 'Oct' , 'Nov' , 'Dec' );
* this var contains the current locale this instace works with
* @var string this is a string like 'de_DE' or 'en_US', etc.
* the locale object which contains all the formatting specs
var $_currentFormat = I18N_DATETIME_DEFAULT;
var $_currentDateFormat = I18N_DATETIME_DEFAULT;
var $_currentTimeFormat = I18N_DATETIME_DEFAULT;
var $_customFormats = array ();
* Use this method to setup and to retreive the static instance of the I18N_DateTime.
* // setup the object with the proper locale
* I18N_DateTime::singleton($locale);
* // and anywhere in your code you can call the following
* // and you get the instance for this very locale, you specified via singleton()
* $dateTime = I18N_DateTime::singleton();
* @param string the locale to use, i.e. 'de_DE'
* @return object an instance of this class
* returns the timestamp formatted according to the locale and the format-mode
* use this method to format a date and time timestamp
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param int the formatting mode, using setFormat you can add custom formats
* @return string the formatted timestamp
function format($timestamp=null ,$format=null )
return $this->_format ($timestamp,$format);
* returns the timestamp formatted according to the locale and the format-mode
* use this method to get a formatted date only
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param int the formatting mode, use setDateFormat to add custom formats
* @return string the formatted timestamp
function formatDate( $timestamp=null , $format=null )
return $this->_formatDateTime ($timestamp,$format,'date');
* returns the timestamp formatted according to the locale and the format-mode
* use this method to get a formatted time only
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param int the formatting mode, use setTimeFormat to add custom formats
* @return string the formatted timestamp
function formatTime( $timestamp=null , $format=null )
return $this->_formatDateTime ($timestamp,$format,'time');
* formats a timestamp consisting of date and time
* or a custom timestamp, which was set using setFormat
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
function _format ( $timestamp , $format )
if (isset ($this->_customFormats[$format])) {
return $this->_translate (date($this->_customFormats[$format],$timestamp));
return $this->_formatDateTime ($timestamp,$format,'date'). ' '.
$this->_formatDateTime ($timestamp,$format,'time');
* this method formats the given timestamp into the given format
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param int the formatting mode, use setTimeFormat to add custom formats
* @param string either 'date' or 'time' so this method knows what it is currently formatting
* @return string the formatted timestamp
function _formatDateTime ($timestamp,$format,$what)
$getFormatMethod = 'get'. ucfirst($what). 'Format';
$format = $this->$getFormatMethod();
$formatArray = $what. 'Formats';
if (isset ($this->_localeObj->{$formatArray}[$format])) {
$curFormat = $this->_localeObj->{$formatArray}[$format];
} elseif (isset ($this->_customFormats [$format])) {
$curFormat = $this->_customFormats [$format];
return $this->_translate (date($curFormat,$timestamp));
* this simply translates the formatted timestamp into the locale-language
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param string a human readable timestamp, such as 'Monday, August 7 2002'
* @return string the formatted timestamp
function _translate ( $string )
//FIXXME optimize this array, use only those that are in the format string, i.e. if no abbreviated formats are used
// dont put the abbreviated's in this array ....
$translateArrays = array ('days','months','daysAbbreviated','monthsAbbreviated');
// this seems a bit difficult i guess,
// but i had problems with the way i did it before, which way simply str_replace the
// src-lang array and the dest-lang array, this caused stuff like
// translating 'Monday' => 'Montag' and then 'Montag' => 'Motag', since 'Mon'
// is the abbreviated weekday for Monday.
// if i would turn it around and translate the abbreviated words first it would screw up worse
// so what i do now is searching for the position of words which can be translated and
// remember the position (using strpos) and dont translate a word at this position a second
// time. this at least prevents the case described above. i hope it covers everything else too
// for me it works quite well now
$translateDest = array ();
$prevPositions = array ();
foreach ($translateArrays as $aArray) {
foreach ($this->{$aArray} as $index=> $aWord) {
if (($pos= strpos($string,$aWord))!==false && !in_array($pos,$prevPositions)) {
$translateSrc[] = $aWord;
$translateDest[] = $this->_localeObj->{$aArray}[$index];
// here we actually replace the strings (translate:-)) that we found, when checking for their position
$string = str_replace($translateSrc,$translateDest,$string);
* define a custom format given by $format and return the $format-id
* the format-id can be used to call format( x , format-id ) to
* tell the method you want to use the format with that id
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param string defines a custom format
* @return int the format-id, to be used with the format-method
function setFormat( $format=I18N_DATETIME_DEFAULT )
* define a custom format given by $format and return the $format-id
* the format-id can be used to call formatDate( x , format-id ) to
* tell the method you want to use the format with that id
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param string defines a custom format
* @return int the format-id, to be used with the format-method
return $this->_setFormat ( $format , 'date' );
* define a custom format given by $format and return the $format-id
* the format-id can be used to call formatTime( x , format-id ) to
* tell the method you want to use the format with that id
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @param string defines a custom format
* @return int the format-id, to be used with the format-method
return $this->_setFormat ( $format , 'time' );
return $this->_currentDateFormat;
return $this->_currentTimeFormat;
* get either the current or the given month name
return $monthNames[$which];
* get all month names for the current locale
* get all month names for the current locale,
* fallback to english if not defined
$propName = 'months'. ($abbreviated ? 'Abbreviated' : '' );
$propName = 'days'. ($abbreviated ? 'Abbreviated' : '' );
// all the following are simply convienence methods
// which make it shorter to call the format methods with the default
// FIXXME we should use overloading here, well with ZE2 we will!!!!
// i am not really happy with the following, since it only bloats the code,
// but the methods make sense :-)
* convinience method, same as format( $timestamp , I18N_DATETIME_SHORT )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_SHORT
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as format( $timestamp , I18N_DATETIME_DEFAULT )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_DEFAULT
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as format( $timestamp , I18N_DATETIME_MEDIUM )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_MEDIUM
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as format( $timestamp , I18N_DATETIME_LONG )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_LONG
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as format( $timestamp , I18N_DATETIME_FULL )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_FULL
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as formatDate( $timestamp , I18N_DATETIME_SHORT )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_SHORT
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as formatDate( $timestamp , I18N_DATETIME_DEFAULT )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_DEFAULT
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as formatDate( $timestamp , I18N_DATETIME_MEDIUM )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_MEDIUM
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as formatDate( $timestamp , I18N_DATETIME_LONG )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_LONG
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as formatDate( $timestamp , I18N_DATETIME_FULL )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_FULL
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as formatTime( $timestamp , I18N_DATETIME_SHORT )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_SHORT
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as formatTime( $timestamp , I18N_DATETIME_DEFAULT )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_DEFAULT
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as formatTime( $timestamp , I18N_DATETIME_MEDIUM )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_MEDIUM
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as formatTime( $timestamp , I18N_DATETIME_LONG )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_LONG
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
* convinience method, same as formatTime( $timestamp , I18N_DATETIME_FULL )
* this method exists, to have a shorter call to the method
* with a default format I18N_DATETIME_FULL
* @author Wolfram Kriesing <wolfram@kriesing.de>
* @return string the formatted timestamp
Documentation generated on Mon, 11 Mar 2019 15:36:11 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|