>  기사  >  php教程  >  PHP实现类似Sina微博发布时间

PHP实现类似Sina微博发布时间

WBOY
WBOY원래의
2016-06-06 20:11:08786검색

public static function timeFormatter($time) { $dt_now=date('Y-m-d H:i:s',time()); $days = DateUtil::interVal("d",$time,$dt_now); $hours = DateUtil::interVal("h",$time,$dt_now); $minutes = DateUtil::interVal("n",$time,$dt_now); $seconds = D

 public static function timeFormatter($time)
    {
        $dt_now=date('Y-m-d H:i:s',time());
        $days = DateUtil::interVal("d",$time,$dt_now);
        $hours = DateUtil::interVal("h",$time,$dt_now);
        $minutes = DateUtil::interVal("n",$time,$dt_now);
        $seconds = DateUtil::interVal("s",$time,$dt_now);
        if ($days == 0 && $hours == 0 && $minutes == 0) {
            return $seconds . "秒前";
        } else if ($days == 0 && $hours == 0) {
            return $minutes . "分钟前";
        } else if ($days == 0) {
            return "今天 " . date("H:i", strtotime($time));
        } else {
            return date("m月d日 H:i", time());
        }
    }
    public static function interVal($interval = "d", $date1, $date2)
    {
        $timedifference = strtotime($date2) - strtotime($date1);
        $days = bcdiv($timedifference, 86400);
        if ($interval == 'd') {
            return $days;
        }
        $temp1 = $timedifference % (86400);
        $hours = bcdiv($temp1, 3600);
        if ($interval == 'h') {
            return $hours;
        }
        $temp2 = $temp1 % (3600);
        $minutes = bcdiv($temp2, 60);
        if ($interval == 'n') {
            return $minutes;
        }
        $seconds = $temp2 % 60;
        if ($interval == 's') {
            return $seconds;
        }
    }
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.