冰豆网

分享网络精彩
bingdou.com.cn

php实例分享之实现显示网站运行时间 - 秒转换年月日时分秒

时间:2021-04-18加入收藏

文章主要介绍了php实现显示网站运行时间,需要的朋友可以参考下,假设你的当前时间为这种格式“2013-01-01”,想计算几天后时间格式,可用的计算代码如下:

本站运行:<?php echo floor((time()-strtotime('2013-01-01'))/86400);?>

输出格式为:
本站运行:1704天

如果你想计算多少年多少月多少日可用下面代码实现:

<?php
date_default_timezone_set('Asia/Shanghai');
function Sec2Time($time){if(is_numeric($time)){$value = array("y"=>0,"m" =>0,"d" =>0,"h" =>0,"i" =>0,"s" =>0,);
if($time>=31556926){$value["y"] = floor($time/31556926);$time=($time%31556926);}
if($time>=2592000){$value["m"]=floor($time/2592000);$time=($time%2592000);}
if($time>=86400){$value["d"]=floor($time/86400);$time=($time%86400);}
if($time>=3600){$value["h"]=floor($time/3600);$time = ($time%3600);}
if($time>=60){$value["i"] = floor($time/60);$time = ($time%60);}
$value["seconds"]=floor($time);return (array) $value;}else{return (bool) FALSE;}}
$site_create_time = strtotime('2013-01-01');
$time = time() - $site_create_time;
$sltime = Sec2Time($time);
echo '本站运行:';if($sltime['y']!='0'){echo $sltime['y'].'年';}if($sltime['m']=='0' && $sltime['d']=='0'){echo '整';}if($sltime['m']!='0'){echo $sltime['m'].'月';}else{if($sltime['d']!='0'){echo '零';}}if($sltime['d']!='0'){echo $sltime['d'].'天';}?>

输出格式为:
本站运行:4年8月3天
以上代码直接放入你想放置的位置即可!!!
打 赏
打赏二维码

TGA: php 技巧

分享到:


官方微信二维码冰豆网官方微信公众号