我怎样才能在两个时间戳之间得到区别

时间:2015-11-17 10:46:21

标签: php mysql

我正在研究一个项目,因为我需要检查两次之间的时差,还需要比较时差。如果时差> 10分钟,我需要执行一些查询..请帮助,我正在处理过去2天。

$db=new Database($dbserver,$dbuser,$dbpassword,$dbname);
$db->connect();
$result=$db->query("SELECT reservedBy, bikeNum FROM bikes WHERE reserveBy
IS NOT NULL");
while ($row=$result->fetch_assoc()) {
    $userid=$row["reservedBy"];
    $bike=$row["bikeNum"];
    $result=$db->query("SELECT time FROM history WHERE userId=$userid AND bikeNum=$bike AND action='RESERVE' ORDER BY time DESC LIMIT 1");
    $row=$result->fetch_assoc();
    $time=$row["time"];
    echo $time. "<br>";
    $date = date('Y-m-d H:i:s ', time());
    echo $date;
    $timediff=date_diff($date$time);
    if($timediff>10) {
         $result=$db->query("UPDATE bikes SET reservedBy=NULL WHERE 
bikeNum=$bike");
    }
}

1 个答案:

答案 0 :(得分:0)

试试这个

function datediff( $date1, $date2 )
{
    $diff = abs( strtotime( $date1 ) - strtotime( $date2 ) );

    return sprintf
    (
        "%d Days, %d Hours, %d Mins, %d Seconds",
        intval( $diff / 86400 ),
        intval( ( $diff % 86400 ) / 3600),
        intval( ( $diff / 60 ) % 60 ),
        intval( $diff % 60 )
    );
}

print datediff( "18th February 2013", "now" ) . "\n";

或者这个:

$start_date = new DateTime("2012-02-10 11:26:00");
$end_date = new DateTime("2012-04-25 01:50:00");
$interval = $start_date->diff($end_date);
echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";