PHP记录网站运行时间?

做这个效果 如何做?

本站运行xxx天,北京时间xxx-xx-xx

假设你的当前时间为这种格式“2009-02-28 16:29:18”,想计算几天后,几小时后,几分钟后,几周,几个月,几年后的时间格式都没有问题,可用的计算方式如下:(限PHP使用)

如果要计算过去的时间,怎么写呢?很简单在数字前面加个“-”号就可以了,当然如果比较复杂的,几小时,几分钟的计算,都可以将时间字符串进行叠加即可!如下例:

怎么样,到这里你是不是学会了呢?
其实很简单有个PHP函数:strtotime
可以任意加减年、月、日,例子:

echo date("Y-m-d",strtotime("+3 day")); 

// 输出:2008-07-13 

echo date("Y-m-d",strtotime("+3 month")); 

// 输出:2008-10-10 

echo date("Y-m-d",strtotime("+3 year")); 

// 输出:2011-07-10

很强大!!记住以后得多熟悉函数,如果不知道这个函数写方法的话,要写很多东西。处理也麻烦。

/* 
* 指定两个时间段,返回不同的时间数 
* $interval:只允许intervals有以下五个值:"w"(周)、"d"(天)、"h"(小时)、"n"(分钟) 和"s"(秒) 
* $date1 通常为当前时间; 
* $date2 需要计算的时间; 
* zhoz.com 0:34 2008-7-13 
*/ 
function DateDiff ($interval = "d", $date1,$date2) { 
// 得到两日期之间间隔的秒数 
$timedifference = strtotime($date2) - strtotime($date1); 
switch ($interval) { 
case "w": $retval = bcdiv($timedifference ,604800); break; 
case "d": $retval = bcdiv( $timedifference,86400); break; 
case "h": $retval = bcdiv ($timedifference,3600); break; 
case "n": $retval = bcdiv( $timedifference,60); break; 
case "s": $retval = $timedifference; break; 
} 
return $retval; 
}

具体看网站qinlulu.com的演示

下例输出七月四日之前的天数:

实例



更多帮助文档http://www.w3school.com.cn/php/php_date.asp

php如何获取当前的运行时间http://blog.csdn.net/qikexun/article/details/50907417

你可能感兴趣的:(编程)