Source for file DateFormatter.class.php
Documentation is available at DateFormatter.class.php
* Including language specific messages.
include_once( CLASSPATH. "core/lang_spec_values/". LANG. ".inc.php" );
echo "<h3>You have to define the constant LANG!</h3>\r\n";
echo "Example for german: define( 'LANG', 'de' );\r\n";
require_once CLASSPATH. "data_structures/ABSTObject.class.php";
* Carries the dayscount of each month.
* @var array $daysPerMonth
$GLOBALS["daysPerMonth"] = array(
* The number of seconds in a minute. This is useful with the calculation of
* dates in unix timestamp.
define( "MINUTEINSECONDS", 60 );
* The number of seconds in a hour. This is useful with the calculation of
* dates in unix timestamp.
define( "HOURINSECONDS", 3600 );
* The number of seconds in a day. This is useful with the calculation of
* dates in unix timestamp.
define( "DAYINSECONDS", 86400 );
* The number of seconds in a week. This is useful with the calculation of
* dates in unix timestamp.
define( "WEEKINSECONDS", 604800 );
* Static methods mainly to compute dates depending on weeknum for industrial
* programs. The base standard is ISO 8601
* (e.g. for german economy time pattern)
* http://en.wikipedia.org/wiki/ISO_8601#Week_dates
* @author Daniel Plücken <daniel@debakel.net>
* @license http://www.gnu.org/copyleft/lesser.html
* GNU Lesser General Public License
* @copyright Copyright (c) 2003 Daniel Plücken <daniel@debakel.net>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA
* Returns a timestamp from ISO date and time
* @author Daniel Plücken <daniel@debakel.net>
* @param string $str_iso_date
$tmp_arr = explode( " ", $str_iso_date );
$ymd_arr = explode( "-", $tmp_arr[0] );
$hms_arr = explode( ":", $tmp_arr[1] );
* Returns a timestamp from a date.
* @todo am and pm is unaccounted yet
* @see http://www.php.net/manual/en/function.mktime.php#67996
* @see http://php.net/date#AEN21898
* @author Daniel Plücken <daniel@debakel.net> (only placed here)
* @param string $strStr The date to konvert to unix timestamp.
* @param string $strPattern The pattern how interpret the date to konvert
function str2time( $strStr, $strPattern = null )
// an array of the valide date characters,
// see: http://php.net/date#AEN21898
// transform the characters array to a string
$strCharacters = implode('', $arrCharacters);
// splits up the pattern by the date characters to get an array of the
// delimiters between the date characters
$arrDelimiters = preg_split('~['. $strCharacters. ']~', $strPattern);
// transform the delimiters array to a string
// splits up the date by the delimiters to get an array of the
$arrStr = preg_split('~['. $strDelimiters. ']~', $strStr);
// splits up the pattern by the delimiters to get an array of the used
$arrPattern = preg_split('~['. $strDelimiters. ']~', $strPattern);
// if the numbers of the two array are not the same, return false,
// because the cannot belong together
// creates a new array which has the keys from the $arrPattern array and
// the values from the $arrStr array
for ($i = 0;$i < count($arrStr);$i++ ) {
$arrTime[$arrPattern[$i]] = $arrStr[$i];
// gernerates a 4 digit year declaration of a 2 digit one by using the
if (isset ($arrTime['y']) && !isset ($arrTime['Y'])) {
$arrTime['Y'] = substr(date('Y'), 0, 2) . $arrTime['y'];
// if a declaration is empty, it will be filled with the current date
foreach ($arrCharacters as $strCharacter) {
if (empty($arrTime[$strCharacter])) {
$arrTime[$strCharacter] = date($strCharacter);
// checks if the date is a valide date
if (!checkdate($arrTime['m'], $arrTime['d'], $arrTime['Y'])) {
* Returns a language specified date based on the constant LANG with the rules
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $timestamp_or_isodate The unix-timestamp or ISO-8601 date
* that should be transformed.
if ( preg_match( "!^(?:\d|[1-9]\d+)$!", $timestamp_or_isodate ) )
for ( $i = 0; $i < strlen( $string ); $i++ )
$char = substr( $string, $i, 1 );
if ( preg_match( "![aAbdgGhHiIjLmnOrsStTUyYzZ]!", $char ) )
$out .= date( $char, $timestamp );
// TODO: isodate from timestamp
// short dayname of the week
$out .= $GLOBALS["monthNames"][$tmp- 1];
// monthname in short form
$out .= utf8_decode( $GLOBALS["monthNamesShort"][$tmp- 1] );
$out .= $GLOBALS["monthNamesShort"][$tmp- 1];
// suffix day numbers ( st, nd, rd, th )
// number of day of the week ( ISO 8601 )
// number of weeknum of the year ( ISO 8601 )
$datetime_arr = explode( " ", $timestamp_or_isodate );
$date_arr = explode( "-", $datetime_arr[0] );
$time_arr = explode( ":", $datetime_arr[1] );
for ( $i = 0; $i < strlen( $string ); $i++ )
$char = substr( $string, $i, 1 );
// ante meridiem and post meridiem from isodate in lower case
$tmp_int = intval( $time_arr[0] );
if ( $tmp_int > 0 && $tmp_int < 13 )
// ante meridiem and post meridiem from isodate in upper case
$tmp_int = intval( $time_arr[0] );
if ( $tmp_int > 0 && $tmp_int < 13 )
// TODO: Swatch internet time from isodate
// TODO: isodate from isodate
// short dayname of the week
// TODO: short weekday name from isodate
// TODO: monthname from isodate
// twelve hour format without leading zero
12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
$out .= $tmp_arr[ intval($time_arr[0]) ];
// twenty four hour format without leading zero
$out .= intval( $time_arr[0] );
// twelve hour format with leading zero
12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,
12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
$tmp_int = intval($time_arr[0]);
// twenty four hour format with leading zero
// minute with leading zero
// whether date is in summertime
// TODO: whether date is in summertime from isodate
// month with leading zero
// monthname in short form
// TODO: monthname in shortform from isodate
// month without leading zero
$out .= intval( $date_arr[1] );
// days count of the month an year from isodate
// suffix day numbers ( st, nd, rd, th )
// number of day of the week ( ISO 8601 )
// TODO: number of day of the week ( ISO 8601 )
// number of weeknum of the year ( ISO 8601 )
// TODO: number of weeknum of the year ( ISO 8601 )
$out .= substr( $date_arr[0], 2 );
* Returns the weekday as number of the week from given unix-timestamp.
* Monday will be returned as zero. Sunday will returned as six.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $timestamp
$temp = date( "w", $timestamp );
return ( $temp == 0 ) ? 6 : $temp - 1;
* Returns the extracted weeknum from a given timestamp.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $timestamp
$wn = date( "W", $timestamp );
$year = date( "Y", $timestamp );
* Returns the extracted year that depends on a weeknum of a unix-timestamp.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $timestamp
$wn = date( "W", $timestamp );
$year = date( "Y", $timestamp );
* Returns the first second of a day from a unix-timestamp.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $timestamp
$day = date( "d", $timestamp );
$month = date( "m", $timestamp );
$year = date( "Y", $timestamp );
return mktime( 0, 0, 0, $month, $day, $year );
* Returns the number of days from given month and year.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $timestamp
// Computig the surplus of the month into the year.
$year = $year + floor( ($month- 1) / 12 );
$month = $month == 0 ? 12 : $month;
$year = $year + floor( ($month- 1) / 12 );
$month = 12 - abs( $month ) % 12;
// Computig the surplus of the month into the year.
return $GLOBALS["daysPerMonth"][ $month - 1 ];
* Returns the timestamp of the week's first second from a given calendar week
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $timestamp
return mktime( 0, 0, 0, $month, $day, $year );
* Returns the monday date of the first calendar week of a given year
* "The first week of a year is the first week which includes at least four
* days in the new year. Another way of putting this is that the first week
* of a year is the week which includes the first Thursday of January.
* It will also include January 4. This means that week 01 could include days
* from the previous year, or that week 53 could include days from the next
* year. For example, 2004-01-01 occurs on a Thursday.
* This means that 2004-W01 consists of 2003-12-29 through 2004-01-04.
* 2005-01-01 occurs on a Saturday, meaning that 2004-W53 is 2004-12-27
* through 2005-01-02, and 2005-W01 starts on 2005-01-03."
* (http://en.wikipedia.org/wiki/ISO_8601#Week_dates)
* @author Daniel Plücken <daniel@debakel.net>
$firstDayOfYearUNIX = mktime( 0, 0, 0, 1, 1, intval( $year ) );
$firstWeekDayOfYear = date( "w", $firstDayOfYearUNIX );
$MonthDayArr = array( 2, 1, 31, 30, 29, 4, 3 );
return $MonthDayArr[ $firstWeekDayOfYear ];
* Returns the date of the monday associated with the given calendar week and
* year as an array. The first entry of the returned array carries the day
* of the month and the second entry carries the month of the given year.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $weeknum
return array( $mondayDate, 12, $year - 1 );
return array( $mondayDate, 1, $year );
// setting right february
$GLOBALS["daysPerMonth"][1] = $leapyear ? 29 : 28;
$mondayDate = $mondayDate - 31;
for ( $i = 2; $i <= $weeknum ; $i++ )
if( $mondayDate + 7 <= $GLOBALS["daysPerMonth"][$month] )
$mondayDate = ( $mondayDate + 7 ) - $GLOBALS["daysPerMonth"][$month];
return array( $mondayDate, 1, $year + 1 );
return array( $mondayDate, $month + 1, $year );
* Returns the date of the given weekday associated with the given calendar
* week and year as an array. The first entry of the returned array carries
* the day of the month and the second entry carries the month of the given
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $weekday
* @param integer $weeknum
// setting right february
$GLOBALS["daysPerMonth"][1] = $leapyear ? 29 : 28;
// change of December to January
$monday[0]+ $weekday <= $GLOBALS["daysPerMonth"][11]
return array( $monday[0]+ $weekday, 12, $year- 1 );
if( $monday[2] == $year- 1 )
return array( $monday[0]+ $weekday- $GLOBALS["daysPerMonth"][11], 1, $year );
if( $monday[0]+ $weekday <= $GLOBALS["daysPerMonth"][$monday[1]- 1] )
return array( $monday[0]+ $weekday, $monday[1], $year );
$dayDate = $monday[0]+ $weekday- $GLOBALS["daysPerMonth"][$monday[1]- 1];
return array( $dayDate, 1, $year+ 1 );
return array( $dayDate, $monday[1]+ 1, $year );
* Returns the date of the tuesday associated with the given calendar week and
* year as an array. The first entry of the returned array carries the day
* of the month and the second entry carries the month of the given year.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $weeknum
* Returns the date of the wednesday associated with the given calendar week
* and year as an array. The first entry of the returned array carries the day
* of the month and the second entry carries the month of the given year.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $weeknum
* Returns the date of the thursday associated with the given calendar week
* and year as an array. The first entry of the returned array carries the day
* of the month and the second entry carries the month of the given year.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $weeknum
* Returns the date of the friday associated with the given calendar week and
* year as an array. The first entry of the returned array carries the day
* of the month and the second entry carries the month of the given year.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $weeknum
* Returns the date of the saturday associated with the given calendar week
* and year as an array. The first entry of the returned array carries the day
* of the month and the second entry carries the month of the given year.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $weeknum
* Returns the date of the sunday associated with the given calendar week and
* year as an array. The first entry of the returned array carries the day
* of the month and the second entry carries the month of the given year.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $weeknum
* Returns weekday name associated with the given timestamp.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $timestamp
return $GLOBALS["dayNames"][$tmp];
* Returns the number of month associated with the given calendar week and
* year. Beware! This function will not return the values between 1 and 12,
* because it could be that a given calendar week is a part of two month.
* Here is the list of the definition:
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $weeknum
if( $sunday[1] < $monday[1] )
return ($sunday[1]- $monday[1]) + $monday[1] * 2 - 2;
* Returns the name of month associated with the given calendar week and year.
* @author Daniel Plücken <daniel@debakel.net>
* @param integer $weeknum
return $GLOBALS["interMonthNames"][
* Returns the number of weeks in a given year.
* @author Daniel Plücken <daniel@debakel.net>
$thisYear_DateOfSundayArr[0] == $nextYear_DateOfSundayArr[0]
&& $thisYear_DateOfSundayArr[1] == $nextYear_DateOfSundayArr[1]
&& $thisYear_DateOfSundayArr[2] == $nextYear_DateOfSundayArr[2]
* Returns whether the given year is a leap year.
* @author Daniel Plücken <daniel@debakel.net>
} // END of class DateFormatter
|