计算MySQL中的datetime字段与当前时间差一例
发表于:2007-07-14来源:作者:点击数:
标签:
注意本程序中使用的表结构为: usetest; createtabledatetime(dtfielddatetime); insertintodatetimevalues(now()); */ ? ? $server=mysql_connect("localhost","test","")ordie("无法连接 数据库 服务器 !"); $db=mysql_select_db("test",$server)ordie("无
注意本程序中使用的表结构为: use test; create table datetime(dtfield datetime); insert into datetime values(now()); */ ?>
<? $server = mysql_connect("localhost","test","") 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("无法与数据库服务器断开连接!"); ?>
|
原文转自:http://www.ltesting.net