计算分钟mysql和php的时差

时间:2014-09-03 07:51:05

标签: mysql time

我在此表中有一个表tem_test,其中一个字段为start_time,其数据类型为DATETIME,我想将此日期与当前日期时间进行比较;如果时间大于30分钟,请选择获取数据....

like

if( user passed 30 minute )
{
    // then update other table with user's info where userid ='user who crossed the 30 minute';
}
else
{
    // do sometihing ....
}
<?php

  <?php

$intranet_connect = mysql_connect('localhost','root',''); //create connection to intranet host
$radius_connect = mysql_connect('localhost','root',''); //creates connection to radius

mysql_select_db('db_intranet',$intranet_connect); //using db_intranet database with $intranet_connect host connection

$query = mysql_query('SELECT username,srvid,start_time FROM temp_day_test  WHERE status="yes"',$intranet_connect); //check if service is on test mode then perform...below

while($row = mysql_fetch_assoc($query)) //fetch selected data from table
{
 $data[]=$row;
}
$data['srvid']= $srvid; //hold service id in $srvid 
$data['username'] = $username;
$data['start_time']=$startTime;

$currentTime = date('Y-m-d H(idea)(worry)'); //create current date time with timezone Asia/kathmandu. NOTE: set time zone in PHP.INI, default.timezone=Asia/Kathmandu

$timeDifference  = (strtotime($currentTime) - strtotime($startTime)); //Calculate time difrence between test start time and test end time



if($timeDifference >1800)// if time exceeds 30 minute then...
{
    echo $data['username'];
    echo $data['srvid'];exit;
 mysql_select_db('radius',$radius_connect); //select radius database 
 $query = mysql_query('UPDATE rm_users SET srvid="'.$srvid.'" WHERE srvid= 44 ',          $radius_connect);
 if(!$query)
 {
  die(mysql_error());
 }
}


 ?>

1 个答案:

答案 0 :(得分:0)

您可以在php中使用date_diff功能。或者MySQL中的TimeDiff(),您可以选择最适合您的方式。