根据时间戳 获取相对应的日期



(1)    function format_date($time){
        $t=time()-$time;
        
        $f=array(
            '31536000'=>'年',
            '2592000'=>'个月',
            '604800'=>'星期',
            '86400'=>'天',
            '3600'=>'小时',
            '60'=>'分钟',
            '1'=>'秒'
        );
        foreach ($f as $k=>$v){
            if (0 !=$c=floor($t/(int)$k)) {
                return $c.$v;
            }
        }
    }




(2)
    function  T($time)
    {
        //获取今天凌晨的时间戳
        $day  =  strtotime ( date ( 'Y-m-d' ,time()));
        //获取昨天凌晨的时间戳
        $pday  =  strtotime ( date ( 'Y-m-d' , strtotime ( '-1 day' )));
        //获取现在的时间戳
        $nowtime  = time();
        
        $tc  =  $nowtime - $time ;
        if ( $time < $pday ){
        $str  =  date ( 'Y-m-d H:i:s' , $time );
        } elseif ( $time < $day  &&  $time > $pday ){
        $str  =  "昨天" ;
        } elseif ( $tc >60*60){
        $str  =  floor ( $tc /(60*60)). "小时前" ;
        } elseif ( $tc >60){
        $str  =  floor ( $tc /60). "分钟前" ;
        } else {
        $str  =  "刚刚" ;
        }
        return  $str ;
    }


(3)
  function timeago() {
      $ptime = '2013-11-11 11:11:11';
        $ptime = strtotime($ptime);
        $etime = time() - $ptime;
        if ($etime < 1) return '刚刚';
        $interval = array (
            12 * 30 * 24 * 60 * 60 => '年前 ('.date('Y-m-d', $ptime).')',
            30 * 24 * 60 * 60 => '个月前 ('.date('m-d', $ptime).')',
            7 * 24 * 60 * 60 => '周前 ('.date('m-d', $ptime).')',
            24 * 60 * 60 => '天前',
            60 * 60 => '小时前',
            60 => '分钟前',
            1 => '秒前'
        );
        foreach ($interval as $secs => $str) {
          $d = $etime / $secs;
          if ($d >= 1) {
            $r = round($d);
            return $r . $str;
          }
        };
    }

你可能感兴趣的:(PHP,php)