| // +---------------------------------------------------------------------------+ /** * Provides various date formatting methods. * * @package SGL * @author Demian Turner * @version $Revision: 1.14 $ */ class SGL_Date { /** * Returns current time in YYYY-MM-DD HH:MM:SS format. * * GMT format is best for logging system events, otherwise locale offset * will be most helpful to users. * * @access public * @static * @param boolean $gmt is time GMT or locale offset * @return string $time formatted current time */ function getTime($gmt = false) { // no logMessage allowed here $time = ($gmt) ? gmstrftime("%Y-%m-%d %H:%M:%S", time()) : strftime("%Y-%m-%d %H:%M:%S", time()); return $time; } /** * Converts date array into MySQL datetime format. * * @access public * @param array $aDate * @return string MySQL datetime format * @see publisher::ArticleMgr::process/edit */ function arrayToString($aDate) { SGL::logMessage(null, PEAR_LOG_DEBUG); if (is_array($aDate)) { $month = $aDate['month']; $day = $aDate['day']; $year = $aDate['year']; $hour = (array_key_exists('hour',$aDate))? $aDate['hour'] : '00'; $minute = (array_key_exists('minute',$aDate))? $aDate['minute'] : '00'; $second = (array_key_exists('second',$aDate))? $aDate['second'] : '00'; if (empty($month) && empty($year) && empty($day)) { return null; } else { return $year . '-' . $month . '-' . $day .' ' . $hour . ':' . $minute . ':' . $second; } } } /** * Converts date into date array. * * @access public * @param string $sDate date (may be in the ISO, TIMESTAMP or UNIXTIME format) format * @return array $aDate * @see publisher::ArticleMgr::process/edit */ function stringToArray($sDate) { SGL::logMessage(null, PEAR_LOG_DEBUG); if (is_scalar($sDate)) { require_once 'Date.php'; $date = & new Date($sDate); $aDate = array('day' => $date->getDay(), 'month' => $date->getMonth(), 'year' => $date->getYear(), 'hour' => $date->getHour(), 'minute' => $date->getMinute(), 'second' => $date->getSecond()); return $aDate; } } /** * Converts date (may be in the ISO, TIMESTAMP or UNIXTIME format) into "Mar 31, 2003 18:29". * * @access public * @param string $date Date (may be in the ISO, TIMESTAMP or UNIXTIME format) value * @return string $formatted user-friendly format (european) */ function formatPretty($date) { if (is_string($date)) { require_once 'Date.php'; $date = & new Date($date); if ($_SESSION['aPrefs']['dateFormat'] == 'FR') { $output = $date->format('%d %B %Y, %H:%M'); } elseif ($_SESSION['aPrefs']['dateFormat'] == 'BR') { // Brazilian date format $output = $date->format('%d de %B de %Y %H:%M'); } elseif ($_SESSION['aPrefs']['dateFormat'] == 'DE') { // German date format $output = $date->format('%d. %B %Y %H:%M'); } else { // else UK and US $output = $date->format('%B %d, %Y %H:%M'); } return $output; } else { SGL::raiseError('no input date passed to SGL_Date::formatPretty incorrect type', SGL_ERROR_INVALIDARGS); } } /** * Converts date (may be in the ISO, TIMESTAMP or UNIXTIME format) into locale dependent form. * * @access public * @param string $input date (may be in the ISO, TIMESTAMP or UNIXTIME format) value * @return string $output user-friendly format (locale dependent) */ function format($date) { if (is_string($date)) { include_once 'Date.php'; $date = & new Date($date); // Neither elegant nor efficient way of doing that // (what if we have 30 formats/locales?). // We should move that to a language/locale dependent file. if ($_SESSION['aPrefs']['dateFormat'] == 'UK') { $output = $date->format('%d.%m.%Y'); } elseif ($_SESSION['aPrefs']['dateFormat'] == 'BR' || $_SESSION['aPrefs']['dateFormat'] == 'FR') { // Brazilian/French date format $output = $date->format('%d/%m/%Y'); } elseif ($_SESSION['aPrefs']['dateFormat'] == 'US') { $output = $date->format('%m.%d.%Y'); } elseif ($_SESSION['aPrefs']['dateFormat'] == 'DE') { $output = $date->format('%d.%m.%Y'); } else { // else display ISO (international, unambiguous) format, YYYY-MM-DD $output = $date->format('%Y-%m-%d'); } return $output; } else { SGL::raiseError('no input date passed to SGL_Date::format incorrect type', SGL_ERROR_INVALIDARGS); } } /** * Gets appropriate date format * * @access public * @return string $date template (e.g. "%d %B %Y, %H:%M" for FR date format) */ function getDateFormat() { if ($_SESSION['aPrefs']['dateFormat'] == 'UK') { $dateFormat = '%d %B %Y, %H:%M'; } elseif ($_SESSION['aPrefs']['dateFormat'] == 'BR' || $_SESSION['aPrefs']['dateFormat'] == 'FR') { // Brazilian/French date format $dateFormat = '%d %B %Y, %H:%M'; } elseif ($_SESSION['aPrefs']['dateFormat'] == 'US') { $dateFormat = '%B %d, %Y %H:%M'; } elseif ($_SESSION['aPrefs']['dateFormat'] == 'DE') { $dateFormat = '%d.%B.%Y %H:%M'; } else { // else display ISO (international, unambiguous) format, YYYY-MM-DD $dateFormat = '%Y-%B-%d'; } return $dateFormat; } /** * Generates a select of day values. * * @access public * @param string $selected * @return string $day_options select day options * @see showDateSelector() */ function getDayFormOptions($selected = '') { SGL::logMessage(null, PEAR_LOG_DEBUG); $day_options = ''; for ($i = 1; $i <= 31; $i++) { $day_options .= "\n