我想以秒为单位获得时间差异。我使用以下代码来获得差异,但它给了我一些奇怪的输出。
$databasetime = strtotime('21:30:45');
$curtime = time();
echo $curtime - $databasetime;
例如,在写这个问题时,它在我的笔记本电脑上是 21:31,所以如果运行这个脚本,期望的结果应该在 60 左右,但我得到的值是 -19615。
我不知道它是怎么计算的?我在下午的堆栈溢出上问过类似的问题,但我的问题还没有解决。
如何在 PHP 中以秒为单位获得时间差异?
请帮助我解决我的问题,因为这一小段代码占用了我一整天的时间。
$databasetime = strtotime('21:30:45');
$curtime = time();
$timeDiff= $curtime - $databasetime;
echo round(abs($timeDiff) / 60,2). " minute";
获取 IST 时间,
echo "Original Time: ". date("h:i:s")."'n"
date_default_timezone_set('Asia/Kolkata');
echo "Indian Standart time Time: ". date("h:i:s")."'n";
对于时差,请使用 diff
$datetime1 = new DateTime('10:35:56 2013-11-15');
$datetime2 = new DateTime('10:35:50 2013-11-15');
$interval = $datetime1->diff($datetime2);
echo $interval->m . " Month " .$interval->d ." Days ". $interval->h . " Hours, " . $interval->i." Mintues, ".$interval->s." seconds <br/>";
另一种方式
$hour_one = "21:30:45";
$h = strtotime($hour_one);
$h2 = time(); //current time
$minute = date("i", $h);
$second = date("s", $h);
$hour = date("H", $h);
$convert = strtotime("-$minute minutes", $h2);
$convert = strtotime("-$second seconds", $convert);
$convert = strtotime("-$hour hours", $convert);
$new_time = date('H:i:s', $convert);
echo $new_time;