Home >Backend Development >PHP Tutorial > php时间调用有关问题

php时间调用有关问题

WBOY
WBOYOriginal
2016-06-13 13:31:39935browse

php时间调用问题
对php编程一点不懂,请教:
想实现,*分钟前(*小时前...),不知道怎么编,在网上找到一段代码,很好用,也很简单:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php function format_time($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.'前';   
        }   
    }   
}   
echo format_time('1336880454');   
?>



但是这里时间需要转换为Unix时间戳,也找到一段代码:
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php $time1 = "2012-05-13 11:40:54";
$time2 = strtotime($time1);
echo $time2;//转换成时间戳格式
echo date('',$time2);//转换成标准日期格式
?>



现在页面的代码是这样的:
PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
<?php while ($row = mysql_fetch_array($rs_tmmsm)) {?>
=date("Y-m-d <!--h:i-->",strtotime($row["pay_time"]))?>
<?php }?>



问题:想把上面三个代码整合在一起,实现我需要的,*分钟前(*小时前...),该如何搞?

------解决方案--------------------
PHP code
<?php function format_time($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.'前';   
        }   
    }   
}  
while ($row = mysql_fetch_array($rs_tmmsm)) {
    echo format_time(strtotime($row["pay_time"]));
}
?> <div class="clear">
                 
              
              
        
            </div>
Statement:
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