Source for file DateUtil.class.php
Documentation is available at DateUtil.class.php
* Zikula Application Framework
* @copyright Robert Gasch
* @link http://www.zikula.org
* @version $Id: DateUtil.class.php 24342 2008-06-06 12:03:14Z markwest $
* @license GNU/GPL - http://www.gnu.org/copyleft/gpl.html
* @author Robert Gasch rgasch@gmail.com
* @uses generic data utililty class
* constants for this class
define ('DATEFORMAT_FIXED', '%Y-%m-%d %H:%M:%S');
* Return a formatted datetime for the given timestamp (or for now)
* @param time The timestamp (string) which we wish to format (default==now)
* @param format The format to use when formatting the date (optional)
* @return datetime The datetime formatted according to the specified format
function getDatetime ($time= '', $format= DATEFORMAT_FIXED)
// bp 2008-03-29 - translation of day and month names if Windows utf-8 system is used
$cs == 'utf-8' ? $cs = 'utf8' : '';
// applies only if $encoding is set, iconv is available and we're using utf8
if ((!empty($encoding)) && ($cs == 'utf8')) {
// check for iconv - not included in std. win distribution
$dtstr = iconv($encoding, $cs, $dtstr);
* Reformat a given datetime according to the specified format
* @param datetime The (string) datetime to reformat
* @param format The format to use when formatting the date (optional)
* @return datetime The datetime formatted according to the specified format
if ($datetime === null) {
* Build a datetime string from the supplied fields
* @param hour The hour (optional) (default==0)
* @param minute The minute (optional) (default==0)
* @param second The second (optional) (default==0)
* @param format The format to use when formatting the date (optional)
* @return datetime The datetime formatted according to the specified format
function buildDatetime ($year, $month, $day, $hour= 0, $minute= 0, $second= 0, $format= DATEFORMAT_FIXED)
$dTime = mktime ($hour, $minute, $second, $month, $day, $year);
* Return a formatted datetime at the end of the business day n days from now
* @param days The number of days to advance (optional) (detault==1)
* @param format The format to use when formatting the date (optional)
* @param hour The hour of the target time to set (optional) (default=null, means params is taken from now)
* @param minute The minute of the target time to set (optional) (default=null, means params is taken from now)
* @param second The second of the target time to set (optional) (default=null, means params is taken from now)
* @return datetime The datetime formatted according to the specified format
function getDatetime_NextDay ($num= 1, $format= DATEFORMAT_FIXED, $year= null, $month= null, $day= null, $hour= null, $minute= null, $second= null)
$next = mktime ($hour!= null ? (int) $hour : date('H'),
$minute!= null ? (int) $minute : date('i'),
$second!= null ? (int) $second : date('s'),
$month!= null ? (int) $month : date('m'),
$day!= null ? (int) $day+ $num : date('d')+ $num,
$year!= null ? (int) $year : date('y'));
* Return a formatted datetime at the end of the business day n week from now
* @param num The number of weeks to advance (optional) (detault==1)
* @param format The format to use when formatting the date (optional)
* @param year The year of the target time to set (optional) (default=null, means param is taken from now)
* @param month The month of the target time to set (optional) (default=null, means param is taken from now)
* @param day The day of the target time to set (optional) (default=null, means param is taken from now)
* @param hour The hour of the target time to set (optional) (default=null, means param is taken from now)
* @param minute The minute of the target time to set (optional) (default=null, means param is taken from now)
* @param second The second of the target time to set (optional) (default=null, means param is taken from now)
* @return datetime The datetime formatted according to the specified format
function getDatetime_NextWeek ($num= 1, $format= DATEFORMAT_FIXED, $year= null, $month= null, $day= null, $hour= null, $minute= null, $second= null)
$next = mktime ($hour!= null ? (int) $hour : date('H'),
$minute!= null ? (int) $minute : date('i'),
$second!= null ? (int) $second : date('s'),
$month!= null ? (int) $month : date('m'),
$day!= null ? (int) $day+ $num : date('d')+ $num,
$year!= null ? (int) $year : date('y'));
* Return a formatted datetime at the end of the business day n months from now
* @param num The number of months to advance (optional) (default=1)
* @param format The format to use when formatting the date (optional)
* @param year The year of the target time to set (optional) (default=null, means param is taken from now)
* @param month The month of the target time to set (optional) (default=null, means param is taken from now)
* @param day The day of the target time to set (optional) (default=null, means param is taken from now)
* @param hour The hour of the target time to set (optional) (default=null, means params is taken from now)
* @param minute The minute of the target time to set (optional) (default=null, means params is taken from now)
* @param second The second of the target time to set (optional) (default=null, means params is taken from now)
* @return datetime The datetime formatted according to the specified format
function getDatetime_NextMonth ($num= 1, $format= DATEFORMAT_FIXED, $year= null, $month= null, $day= null, $hour= null, $minute= null, $second= null)
$next = mktime ($hour!= null ? (int) $hour : date('H'),
$minute!= null ? (int) $minute : date('i'),
$second!= null ? (int) $second : date('s'),
$month!= null ? (int) $month : date('m')+ $num,
$day!= null ? (int) $day+ $num : date('d'),
$year!= null ? (int) $year : date('y'));
* Return a formatted datetime at the end of the business day n years from now
* @param num The number of years to advance (optional) (default=1)
* @param format The format to use when formatting the date (optional)
* @param year The year of the target time to set (optional) (default=null, means param is taken from now)
* @param month The month of the target time to set (optional) (default=null, means param is taken from now)
* @param day The day of the target time to set (optional) (default=null, means param is taken from now)
* @param hour The hour of the target time to set (optional) (default=null, means params is taken from now)
* @param minute The minute of the target time to set (optional) (default=null, means params is taken from now)
* @param second The second of the target time to set (optional) (default=null, means params is taken from now)
* @return datetime The datetime formatted according to the specified format
function getDatetime_NextYear ($num= 1, $format= DATEFORMAT_FIXED, $year= null, $month= null, $day= null, $hour= null, $minute= null, $second= null)
$next = mktime ($hour!= null ? (int) $hour : date('H'),
$minute!= null ? (int) $minute : date('i'),
$second!= null ? (int) $second : date('s'),
$month!= null ? (int) $month : date('m'),
$day!= null ? (int) $day+ $num : date('d'),
$year!= null ? (int) $year : date('y')+ $num);
* Return the date portion of a datetime timestamp
* @param datetime The date to parse (optional) (default=='', reverts to now)
* @param format The format to use when formatting the date (optional)
* @return string The Date portion of the specified datetime
$spaceOffset = strpos ($datetime, ' ');
return substr ($sTime, 0, $spaceOffset);
* Return the time portion of a datetime timestamp
* @param datetime The date to parse (optional) (default=='', reverts to now)
* @param format The format to use when formatting the date (optional)
* @return string The Time portion of the specified datetime
$spaceOffset = strpos ($datetime, ' ');
return substr ($datetime, $spaceOffset+ 1);
* Return the requested field from the supplied date
* Since the date fields can change depending on the date format,
* the following convention is used when referring to date fields:<br />
* Field 5 -> Minute<br />
* Field 6 -> Second<br />
* @param datetime The field number to return
* @param field The date to parse (default=='', reverts to now)
* @return string The requested datetime field
$field-- ; // adjust for human counting
if ($field <= 2) // looking for a date part
else // looking for a time part
* Return an structured array holding the differences between 2 dates.
* The returned array will be structured as follows:<br>
* [d] => _numeric_day_value_<br />
* [h] => _numeric_hour_value_<br />
* [m] => _numeric_minute_value_<br />
* [s] => _numeric_second_value_ )<br />
* @param date1 The first date
* @param date2 The second date
* @return array The structured array containing the datetime difference
return array("d"=> $d,"h"=> $h,"m"=> $m,"s"=> $s);
* Return an field holding the differences between 2 dates expressed
* in units of the field requested
* Since the date fields can change depending on the date format,
* the following convention is used when referring to date fields:<br />
* Field 5 -> Minute<br />
* Field 6 -> Second<br />
* @param date1 The first date
* @param date2 The second date
* @param field The field (unit) in which we want the different
* @return float The difference in units of the specified field
$diff = $s/ (60* 60* 24* 31* 12);
$diff = $s/ (60* 60* 24* 31);
* Calculate day-x of KW in a YEAR
* @param day 0 for monday, 6 for sunday,....
* @param kw week of the year
* @param flag u or s (unixtimestamp or MySQLDate)
* @return unixtimestamp or sqlDate
$wday = date('w',mktime(0,0,0,1,1,$year)); // 1=Monday,...,7 = Sunday
$firstday = mktime(0,0,0,1,1- ($wday- 1)+ $day,$year);
$firstday = mktime(0,0,0,1,1+ (7- $wday+ 1)+ $day,$year);
$month = date('m',$firstday);
$year = date('Y',$firstday);
$day = date('d',$firstday);
$return = mktime(0,0,0,$month,$day+ $adddays,$year);
* Return a the number of days in the given month/year
* @param month The (human) month number to check
* @param year The year number to check
* @return integer The number of days in the given month/year
if ($month < 1 || $month > 12) {
$days = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
// Check for leap year, no 4000 rule
* Return an array of weekdays for the given month
* @param month The (human) month number to check
* @param year The year number to check
* @return integer The number of days in the given month/year
for ($i= 1; $i<= $nDays; $i++ ) {
$time = mktime (12, 0, 0, $month, $i, $year);
$weekdays[$i] = $tDate['wday'];
* Return an array of dates for the given month
* @param month The (human) month number to check
* @param year The year number to check
* @return integer The number of days in the given month/year
for ($i= 1; $i<= $days; $i++ ) {
* Parses a user interface date string (excluding time) into a timestamp
* Currently only the format YYYY-MM-DD is supported, but future versions
* will be able to parse more diverse formats.
* @param text The UI date string
* @return date The timestamp or null in case of errors
$elements = split('-', $text);
if (count($elements) == 3)
list ($year,$month,$day) = $elements;
$year = $month = $day = 0;
return mktime(0,0,0, $month,$day,$year);
* Parses a user interface date+time string into a timestamp
* Currently only the format YYYY-MM-DD HH:MM is supported, but future versions
* will be able to parse more diverse formats.
* @param text The UI date+time string
* @return date The timestamp or null in case of errors
if (preg_match("/(\\d+)-(\\d+)-(\\d+)(\s+(\\d+)(:(\\d+))?)?/", $text, $matches))
if (count($matches) >= 4)
if (count($matches) >= 5)
if (!checkdate($month,$day,$year) || $hour> 23 || $min> 59)
return mktime($hour,$min,$sec, $month,$day,$year);
* create a unix timestamp from either a unix timestamp (sic!), a MySQL timestamp
* or a string. This code is taken from smarty_make_timestamp.php,
* credits go to Monte Ohrt <monte at ohrt dot com>
* We use a copy of the code here due to performance reasons.
* @param int or text a timestamp in one of the formats mentioned
* @return int a unix timestamp
// it is mysql timestamp format of YYYYMMDDHHMMSS?
// it is a numeric string, we handle it as timestamp
// strtotime should handle it
if ($time == - 1 || $time === false) {
// strtotime() was not able to parse $string, use "now":
|