网站建设资讯

NEWS

网站建设资讯

javascript、php与mysql时间格式化函数

  1. javascript:
    /*
    @desc:时间友好显示函数
    @param stamp 时间缀,10位长度
    */
    function timefriend(stamp){
    this.stamp = stamp
    /*
     @desc:主方法,执行转换
     */
    this.get = function(){
        var stamp = this.stamp
        if(!stamp){
            return '—';
        }
        var date = new Date(stamp*1000)
        var time = Math.round(new Date().getTime()/1000)
        diff = time - stamp
        if(diff<0){
            return '—';
        }else if(diff<60){
            return diff+'秒前'
        }else if(diff<3600){
            return Math.floor(diff/60)+'分钟前'
        }else if(diff<86400){
            return Math.floor(diff/3600)+'小时前'
        }else if(diff<259200){
            return Math.floor(diff/86400)+'天前'
        }else{
            var year = this.parsetime(date.getFullYear())
            var month = this.parsetime(date.getMonth())
            var day = this.parsetime(date.getDate())
            var hour = this.parsetime(date.getHours())
            var minute = this.parsetime(date.getMinutes())
            var second = this.parsetime(date.getSeconds())
            var ret = year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second
            return ret
        }
    }
    this.parsetime = function(input){
        var ret
        if(input >= 0 && input < 10){
            ret = '0'+input
        }else{
            ret = input
        }
        return ret
    }
    }
    var timefriend = new timefriend('1428593779')
    console.log(timefriend.get())
  2. php:
    /*
    @desc:显示某一个时间相当于当前时间在多少秒前,多少分钟前,多少小时前
    @param  stamp      时间戳
    @param  format     时间显示格式,默认Y-m-d H:i:s
    @return 如      5秒前
    */
    function timefriend($stamp,$format = 'Y-m-d H:i:s'){
    if(empty($stamp)||!is_numeric($stamp)||!$stamp){
        return '—';
    }
    $diff = time() - $stamp;
    if($diff<0){
        return '—';
    }elseif($diff<60){
        return $diff.'秒前';
    }elseif($diff<3600){
        return floor($diff/60).'分钟前';
    }elseif($diff<86400){
        return floor($diff/3600).'小时前';
    }elseif($diff<259200){
        return floor($diff/86400).'天前';
    }else{
        return date($format,$stamp);
    }
    }
  3. MySQL:
    DELIMITER //
    CREATE FUNCTION friendlyDate(sTime char(11))
    RETURNS char(11)
    BEGIN
    if sTime='' then
        return '-';
    end if;
    set @cTime=UNIX_TIMESTAMP();
    set @dTime=@cTime-sTime;
    set @dDay=DATE_FORMAT(@cTime,'%e')-DATE_FORMAT(@sTime,'%e');
    set @dYear=DATE_FORMAT(@cTime,'%Y')-DATE_FORMAT(@sTime,'%Y');
    if @dTime>0 then
        if @dTime<60 then
            if @dTime<10 then
                return '刚刚';
            else
                set @t=floor(@dTime/10)*10;
                set @ret=concat(@t,'秒前');
                return @ret;
            end if;
        elseif @dTime<3600 then
            set @t=@dTime/60;
            set @ret=concat(@t,'分钟前');
            return @ret;
        elseif @dYear=0 && @dDay=0 then
            set @t=DATE_FORMAT(@sTime,'%H:%i');
            set @ret=concat('今天',@t);
            return @ret;
        elseif @dYear=0 then
            return DATE_FORMAT(@sTime,'%m月%d日 %H:%i');
        else
            return DATE_FORMAT(@sTime,'%Y-%m-%d %H:%i:%s');
        end if;
    else
        return DATE_FORMAT(@sTime,'%Y年%m月%d日 %H:%i');
    end if;
    END
    //
    DELIMITER ;

网页标题:javascript、php与mysql时间格式化函数
本文URL:http://cdweb.net/article/jcoijj.html