---
title: "PHP 将发布时间修改为中文提示"
author: "onestopweb"
date: "2017-09-27T17:39:40Z"
tags: []
categories: ["编程语言"]
slug: "php"
draft: false
lastmod: "2017-09-27T17:39:40Z"
id: 2394731
url: "https://onestopweb.iteye.com/blog/2394731"
---

```
<?php
header('content-type:text/html;charset=utf-8');
function transferTime($time){
    $rtime = date("m-d H:i", $time);
    $htime = date("H:i", $time);
    $time = time() - $time;
    if ($time < 60) {
        $str = '刚刚';
    } elseif ($time < 60 * 60) {
        $min = floor($time / 60);
        $str = $min . '分钟前';
    } elseif ($time < 60 * 60 * 24) {
        $h = floor($time / (60 * 60));
        $str = $h . '小时前 ' . $htime;
    } elseif ($time < 60 * 60 * 24 * 3) {
        $d = floor($time / (60 * 60 * 24));
        if ($d == 1)
            $str = '昨天 ' . $rtime;
            else
                $str = '前天 ' . $rtime;
    } else {
        $str = $rtime;
    }
    return $str;
}
$date = "1506505033";
echo transferTime($date);
```

 

效果图：  
![](http://dl2.iteye.com/upload/attachment/0127/1636/a9cb3555-29a3-38de-aaf8-a8993c0edc39.png)