我正在尝试从时间输入中计算时间,但结果将始终是整数,例如16:45-8:30不会产生8:15或8.15的结果,但是只有8且我找不到解决方案。 这是代码:
<?php
if(isset($_POST['a_submit'])){
$begin = $_POST['begin'];
$end = $_POST['end'];
$test = $end - $begin;
echo $test;
} ?>
<form class="container mt-5" action="#" method="post">
<div class="form-group row">
<label for="begin" class="col-sm-4 col-form-label">begin</label>
<div class="col-sm-8">
<input type="time" class="form-control-plaintext" id="staticEmail" name="begin">
</div>
</div>
<div class="form-group row">
<label for="end" class="col-sm-4 col-form-label">end</label>
<div class="col-sm-8">
<input type="time" class="form-control-plaintext" id="staticEmail" name="end">
</div>
</div>
<button class="btn btn-primary" name="a_submit" type="submit">go</button>
</form>
感谢您的帮助
答案 0 :(得分:2)
您要减去时间字符串值,而是使用以下代码来获取时差。
$a = new DateTime($_POST['begin']);
$b = new DateTime($_POST['end']);
$b->modify('-1 hours'); // subtract 1 hour break time from endtime
$interval = $a->diff($b);
echo $interval->format("%H:%i");
也请参见此处 Subtract time in PHP