示例#1
0
文件: legacy.php 项目: rhymix/rhymix
/**
 * Convert a Unix timestamp to YYYYMMDDHHIISS format, using the internal time zone.
 * If the timestamp is not given, the current time is used.
 * 
 * @param int $timestamp Unix timestamp
 * @return string
 */
function getDisplayDateTime($timestamp = null, $format = 'YmdHis')
{
    $timestamp = $timestamp !== null ? $timestamp : time();
    return Rhymix\Framework\DateTime::formatTimestampForCurrentUser($format, $timestamp);
}
示例#2
0
/**
 * Convert YYYYMMDDHHIISS format to user-defined format.
 * This function assumes the internal timezone.
 *
 * @param string $str Time in YYYYMMDDHHIISS format
 * @param string $format Time format for date() function
 * @param bool $conversion If true, convert automatically for the current language.
 * @return string
 */
function zdate($str, $format = 'Y-m-d H:i:s', $conversion = false)
{
    if (!$str) {
        return null;
    }
    // convert the date format according to the language
    if ($conversion) {
        static $convtable = array('en' => array('Y-m-d' => 'M j, Y', 'Y-m-d H:i:s' => 'M j, Y H:i:s', 'Y-m-d H:i' => 'M j, Y H:i'), 'es' => array('Y-m-d' => 'j M Y', 'Y-m-d H:i:s' => 'j M Y H:i:s', 'Y-m-d H:i' => 'j M Y H:i'), 'de' => 'es', 'fr' => 'es', 'vi' => array('Y-m-d' => 'd-m-Y', 'Y-m-d H:i:s' => 'H:i:s d-m-Y', 'Y-m-d H:i' => 'H:i d-m-Y'));
        $lang_type = Context::getLangType();
        if (isset($convtable[$lang_type])) {
            if (isset($convtable[$lang_type][$format])) {
                $format = $convtable[$lang_type][$format];
            } elseif (is_string($convtable[$lang_type]) && isset($convtable[$convtable[$lang_type]][$format])) {
                $format = $convtable[$convtable[$lang_type]][$format];
            }
        }
    }
    // get unixtime by using ztime() for date() function's argument.
    $result = Rhymix\Framework\DateTime::formatTimestampForCurrentUser($format, ztime($str));
    // change day and am/pm for each language
    if (preg_match('/[MFAa]/', $format)) {
        $unit_week = (array) Context::getLang('unit_week');
        $unit_meridiem = (array) Context::getLang('unit_meridiem');
        $result = str_replace(array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'), $unit_week, $result);
        $result = str_replace(array('am', 'pm', 'AM', 'PM'), $unit_meridiem, $result);
    }
    return $result;
}