示例#1
0
function smarty_function_mtdate($args, &$ctx)
{
    require_once "MTUtil.php";
    $t = time();
    $ts = offset_time_list($t, $ctx->stash('blog'));
    $args['ts'] = sprintf("%04d%02d%02d%02d%02d%02d", $ts[5] + 1900, $ts[4] + 1, $ts[3], $ts[2], $ts[1], $ts[0]);
    return $ctx->_hdlr_date($args, $ctx);
}
示例#2
0
    function _hdlr_date($args, &$ctx)
    {
        $ts = null;
        if (isset($args['ts'])) {
            $ts = $args['ts'];
        }
        $ts or $ts = $ctx->stash('current_timestamp');
        $ts = preg_replace('![^0-9]!', '', $ts);
        $blog = $ctx->stash('blog');
        if ($ts == '') {
            $t = time();
            if ($args['utc']) {
                $ts = gmtime($t);
            } else {
                $ts = offset_time_list($t, $blog);
            }
            $ts = sprintf("%04d%02d%02d%02d%02d%02d", $ts[5] + 1900, $ts[4] + 1, $ts[3], $ts[2], $ts[1], $ts[0]);
        }
        if (isset($args['utc'])) {
            if (!is_object($blog)) {
                $blog = $ctx->mt->db()->fetch_blog($blog);
            }
            preg_match('/(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)/', $ts, $matches);
            list($all, $y, $mo, $d, $h, $m, $s) = $matches;
            $so = $blog->blog_server_offset;
            $timelocal = mktime($h, $m, $s, $mo, $d, $y);
            $localtime = localtime($timelocal);
            if ($localtime[8]) {
                $so += 1;
            }
            $partial_hour_offset = 60 * abs($so - intval($so));
            $four_digit_offset = sprintf('%s%02d%02d', $so < 0 ? '-' : '+', abs($so), $partial_hour_offset);
            $ts = gmdate('YmdHis', strtotime("{$y}-{$mo}-{$d} {$h}:{$m}:{$s} {$four_digit_offset}"));
        }
        if (isset($args['format_name'])) {
            if ($format = $args['format_name']) {
                $tz = 'Z';
                if (!$args['utc']) {
                    $blog = $ctx->stash('blog');
                    if (!is_object($blog)) {
                        $blog = $ctx->mt->db()->fetch_blog($blog);
                    }
                    $so = $blog->blog_server_offset;
                    $partial_hour_offset = 60 * abs($so - intval($so));
                    if ($format == 'rfc822') {
                        $tz = sprintf("%s%02d%02d", $so < 0 ? '-' : '+', abs($so), $partial_hour_offset);
                    } elseif ($format == 'iso8601') {
                        $tz = sprintf("%s%02d:%02d", $so < 0 ? '-' : '+', abs($so), $partial_hour_offset);
                    }
                }
                if ($format == 'rfc822') {
                    $args['format'] = '%a, %d %b %Y %H:%M:%S ' . $tz;
                    $args['language'] = 'en';
                } elseif ($format == 'iso8601') {
                    $args['format'] = '%Y-%m-%dT%H:%M:%S' . $tz;
                }
            }
        }
        if (!isset($args['format'])) {
            $args['format'] = null;
        }
        require_once "MTUtil.php";
        $fds = format_ts($args['format'], $ts, $blog, isset($args['language']) ? $args['language'] : null);
        if (isset($args['relative'])) {
            if ($args['relative'] == 'js') {
                preg_match('/(\\d\\d\\d\\d)[^\\d]?(\\d\\d)[^\\d]?(\\d\\d)[^\\d]?(\\d\\d)[^\\d]?(\\d\\d)[^\\d]?(\\d\\d)/', $ts, $match);
                list($xx, $y, $mo, $d, $h, $m, $s) = $match;
                $mo--;
                $js = <<<EOT
<script type="text/javascript">
/* <![CDATA[ */
document.write(mtRelativeDate(new Date({$y},{$mo},{$d},{$h},{$m},{$s}), '{$fds}'));
/* ]]> */
</script><noscript>{$fds}</noscript>
EOT;
                return $js;
            } else {
                preg_match('/(\\d\\d\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)(\\d\\d)/', $ts, $matches);
                list($all, $y, $mo, $d, $h, $m, $s) = $matches;
                $unix_ts = offset_time(gmmktime($h, $m, $s, $mo, $d, $y), $blog, '-');
                $now_ts = time();
                $relative = $this->relative_date($unix_ts, $now_ts, $args['relative'], $blog);
                if (is_array($relative)) {
                    return format_ts($relate['format'], $ts, $blog, isset($args['language']) ? $args['language'] : null);
                } elseif ($relative) {
                    return $relative;
                }
            }
        }
        return $fds;
    }