计算MySQL中的datetime字段与当前时间差一例
时间:2007-02-17 来源:PHP爱好者
注意本程序中使用的表结构为:
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("无法与数据库服务器断开连接!");
?>
php爱好者站 http://www.phpfans.net Linux|Apache|IIS.