gpt4 book ai didi

php - 我怎样才能得到两个时间戳之间的差异

转载 作者:行者123 更新时间:2023-11-29 21:49:50 24 4
gpt4 key购买 nike

我正在做一个项目,因为我需要检查两个时间之间的时差,并且我还需要比较时差。如果时差 >= 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");
}
}

最佳答案

试试这个

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 ";

关于php - 我怎样才能得到两个时间戳之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33754955/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com