zoukankan      html  css  js  c++  java
  • 函数实现(获取刚刚、几分钟前、几小时前、几天前、几月前的时间)

    代码如下(例1):

    <?php
    function time_tran($the_time){ $now_time = date("Y-m-d H:i:s",time()+8*60*60);//8*60*60时区的设置 $now_time = strtotime($now_time); $show_time = strtotime($the_time); $dur = $now_time - $show_time; if($dur < 0){ return $the_time; }else{ if($dur < 60){ return $dur.'秒前'; }else{ if($dur < 3600){ return floor($dur/60).'分钟前';//floor(x),有时候也写做Floor(x),其功能是“向下取整”,或者说“向下舍入”;与floor函数对应的是ceil函数,即上取整函数。 }else{ if($dur < 86400){ return floor($dur/3600).'小时前'; }else{ if($dur < 259200){//3天内 return floor($dur/86400).'天前'; }else{ return $the_time; } } }

    代码如下(例2):

    <?php
    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.'前';
            }
        }
    }
    ?>
    

      

  • 相关阅读:
    centos7安装 mysqlclient 报错的解决办法
    linux yum配置代理
    mysql 基础知识
    centos7 安装MySQL
    win安装mysql
    centos7 安装Mariadb
    python socket
    python 协程
    python 线程
    python 进程
  • 原文地址:https://www.cnblogs.com/gaojunshan/p/6560160.html
Copyright © 2011-2022 走看看