PHP Development Tips (1)-Sample Code for Friendly Display of Time and Date

黄舟
Release: 2023-03-06 13:36:02
Original
1492 people have browsed it

System friendliness is very important in the development process. A very friendly system can not only give users a great experience, but also make the system have a longer life. Today's blog post mainly shows how we can make the date and time display more user-friendly.

The following is the code to make the time and date display friendly:

<?php  
  
/** 
 * ======================================= 
 * Created by Zhihua_W. 
 * Author: Zhihua_W 
 * Date: 2016/11/23 0001 
 * Time: 下午 5:45 
 * Project: PHP开发小技巧 
 * Power: 实现日期时间友好显示 
 * ======================================= 
 */  
  
/** 
 * 日期时间友好显示 
 * @param $time 
 * @return bool|string 
 */  
function friend_date($time)  
{  
    if (!$time) {  
        return false;  
    }  
    $fdate = &#39;&#39;;  
    $d = time() - intval($time);  
    $ld = $time - mktime(0, 0, 0, 0, 0, date(&#39;Y&#39;)); //得出年  
    $md = $time - mktime(0, 0, 0, date(&#39;m&#39;), 0, date(&#39;Y&#39;)); //得出月  
    $byd = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) - 2, date(&#39;Y&#39;)); //前天  
    $yd = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) - 1, date(&#39;Y&#39;)); //昨天  
    $dd = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;), date(&#39;Y&#39;)); //今天  
    $td = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) + 1, date(&#39;Y&#39;)); //明天  
    $atd = $time - mktime(0, 0, 0, date(&#39;m&#39;), date(&#39;d&#39;) + 2, date(&#39;Y&#39;)); //后天  
    if ($d == 0) {  
        $fdate = &#39;刚刚&#39;;  
    } else {  
        switch ($d) {  
            case $d < $atd:  
                $fdate = date(&#39;Y年m月d日&#39;, $time);  
                break;  
            case $d < $td:  
                $fdate = &#39;后天&#39; . date(&#39;H:i&#39;, $time);  
                break;  
            case $d < 0:  
                $fdate = &#39;明天&#39; . date(&#39;H:i&#39;, $time);  
                break;  
            case $d < 60:  
                $fdate = $d . &#39;秒前&#39;;  
                break;  
            case $d < 3600:  
                $fdate = floor($d / 60) . &#39;分钟前&#39;;  
                break;  
            case $d < $dd:  
                $fdate = floor($d / 3600) . &#39;小时前&#39;;  
                break;  
            case $d < $yd:  
                $fdate = &#39;昨天&#39; . date(&#39;H:i&#39;, $time);  
                break;  
            case $d < $byd:  
                $fdate = &#39;前天&#39; . date(&#39;H:i&#39;, $time);  
                break;  
            case $d < $md:  
                $fdate = date(&#39;m月d日 H:i&#39;, $time);  
                break;  
            case $d < $ld:  
                $fdate = date(&#39;m月d日&#39;, $time);  
                break;  
            default:  
                $fdate = date(&#39;Y年m月d日&#39;, $time);  
                break;  
        }  
    }  
    return $fdate;  
}  
  
  
$time = 1442345132;  
echo friend_date($time);  
  
?>
Copy after login


The above is the detailed content of PHP Development Tips (1)-Sample Code for Friendly Display of Time and Date. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template