zoukankan      html  css  js  c++  java
  • discuz 个性化时间函数

    define('DATEFORMAT', 'Y-m-d'); // 日期格式
    define('TIMEFORMAT', 'H:i:s'); //时间格式
    define('DATECONVERT', 1); //是否使用人性化时间格式
    define('TIMEOFFSET', '8'); //默认所在时区
    define('TIMESTAMP', time());
    $lang = array(
        'before' => '前',
        'day' => '天',
        'yday' => '昨天',
        'byday' => '前天',
        'hour' => '小时',
        'half' => '半',
        'min' => '分钟',
        'sec' => '秒',
        'now' => '刚刚',
    );
    
    
    $timestamp = strtotime('2014-3-12 10:20:34');
    $time = dgmdate($timestamp, 'u');
    echo $time;
    
    
    function dgmdate($timestamp, $format = 'dt', $timeoffset = '9999', $uformat = '') {
        global $lang;
        $format == 'u' && !DATECONVERT && $format = 'dt';
        static $dformat, $tformat, $dtformat, $offset;
        if ($dformat === null) {
            $dformat = DATEFORMAT;
            $tformat = TIMEFORMAT;
            $dtformat = $dformat . ' ' . $tformat;
            $offset = TIMEOFFSET;
        }
        $timeoffset = $timeoffset == 9999 ? $offset : $timeoffset;
        $timestamp += $timeoffset * 3600;
        $format = empty($format) || $format == 'dt' ? $dtformat : ($format == 'd' ? $dformat : ($format == 't' ? $tformat : $format));
        if ($format == 'u') {
            $todaytimestamp = TIMESTAMP - (TIMESTAMP + $timeoffset * 3600) % 86400 + $timeoffset * 3600;
            $s = gmdate(!$uformat ? str_replace(":i", ":i:s", $dtformat) : $uformat, $timestamp);
            $time = TIMESTAMP + $timeoffset * 3600 - $timestamp;
            if ($timestamp >= $todaytimestamp) {
                if ($time > 3600) {
                    return '<span title="' . $s . '">' . intval($time / 3600) . '&nbsp;' . $lang['hour'] . $lang['before'] . '</span>';
                } elseif ($time > 1800) {
                    return '<span title="' . $s . '">' . $lang['half'] . $lang['hour'] . $lang['before'] . '</span>';
                } elseif ($time > 60) {
                    return '<span title="' . $s . '">' . intval($time / 60) . '&nbsp;' . $lang['min'] . $lang['before'] . '</span>';
                } elseif ($time > 0) {
                    return '<span title="' . $s . '">' . $time . '&nbsp;' . $lang['sec'] . $lang['before'] . '</span>';
                } elseif ($time == 0) {
                    return '<span title="' . $s . '">' . $lang['now'] . '</span>';
                } else {
                    return $s;
                }
            } elseif (($days = intval(($todaytimestamp - $timestamp) / 86400)) >= 0 && $days < 7) {
                if ($days == 0) {
                    return '<span title="' . $s . '">' . $lang['yday'] . '&nbsp;' . gmdate($tformat, $timestamp) . '</span>';
                } elseif ($days == 1) {
                    return '<span title="' . $s . '">' . $lang['byday'] . '&nbsp;' . gmdate($tformat, $timestamp) . '</span>';
                } else {
                    return '<span title="' . $s . '">' . ($days + 1) . '&nbsp;' . $lang['day'] . $lang['before'] . '</span>';
                }
            } else {
                return $s;
            }
        } else {
            return gmdate($format, $timestamp);
        }
    }
  • 相关阅读:
    [Xcode 实际操作]七、文件与数据-(6 )通过通知中心,实现监听和处理程序退出事件的功能
    [Xcode 实际操作]七、文件与数据-(5 )复制、移动、删除文件和删除文件夹
    [Xcode 实际操作]七、文件与数据-(4 )遍历文件夹中的文件
    [Xcode 实际操作]七、文件与数据-(3)创建文本文件、属性列表文件、图片文件
    自己定义控件-GifView
    HDU 3591 多重背包
    Atitit.列表页面and条件查询的实现最佳实践(2)------翻页 分页 控件的实现java .net php
    HDU1950-Bridging signals-最长上升子序列
    Scilab 的画图函数(2)
    POJ 3984 迷宫问题
  • 原文地址:https://www.cnblogs.com/bandbandme/p/3600043.html
Copyright © 2011-2022 走看看