PHP求时间差

本程序中使用的表结构为:

USE test;
CREATE TABLE datetime(dtfield datetime);
INSERT INTO datetime VALUES(now());

PHP代码为:

< ?php
$server = mysql_connect("localhost","root","root") or die("无法连接数据库服务器!");
$db = mysql_select_db("test",$server) or die("无法连接test数据库!");
$sql = "select unix_timestamp(dtfield) from datetime";
$rst = mysql_query($sql) or die("查询失败:$sql");
$row = mysql_fetch_array($rst) or die("无法取得数据!");
$datetime = $row["unix_timestamp(dtfield)"];
$now = time();
$diff = $now-$datetime;
$diffhour  = (int)($diff/3600);
$diffminute = (int)(($diff-$diffhour*3600)/60);
$diffsecond = $diff-$diffhour*3600-$diffminute*60;
echo "当前时间为: " . date("Y年m月d日 H:i:s",$now) . "<br />";
echo "数据库中的时间为: " . date("Y年m月d日 H:i:s",$datetime) . "<br />";
echo "当前时间与数据库时间相差: " . $diffhour . "时" . $diffminute . "分" . $diffsecond . "秒";
mysql_close($server) or die("无法与数据库服务器断开连接!");
?>

关于Burgess Lee

人能常清静 天地悉皆归
此条目发表在技术相关分类目录,贴了, 标签。将固定链接加入收藏夹。