我在下面有这段代码。
<?php
require_once 'con.php';
$start_time_input = strtotime('08:00:00');
$finish_time_input = strtotime('17:00:00');
$total_time = 0;
$query = mysqli_query($con, "SELECT `start_break`, `finish_break` FROM `break_time` WHERE `start_break` AND `finish_break` BETWEEN '".$start_time_input."' AND '".$finish_time_input."' " );
while($tampil = mysqli_fetch_array($query)){
$start_time_db = strtotime($tampil['start_break']);
$finish_time_db = strtotime($tampil['finish_break']);
if ($start_time_input <= $start_time_db AND $finish_time_input >= $finish_time_db) {
$total_time = (($finish_time_input - $start_time_input) - ($finish_time_db - $start_time_db)) / 3600;
} else {
$total_time = ($finish_time_input - $start_time_input) / 3600;
}
echo $total_time;
}
?>
我尝试执行php文件,页面显示没什么,mysql查询似乎不正确。但是当我尝试在phpmyadmin SELECT start_break, finish_break FROM break_time WHERE start_break AND finish_break BETWEEN '08:00:00' AND '17:00:00'
中运行此查询时,查询会按预期给出结果。
任何人都可以帮我这个吗?我曾尝试做一些研究,但据我所知,我只需要将 strtotime 添加到我的变量中,我已经这样做了,没有任何反应。
非常感谢任何帮助。
答案 0 :(得分:5)
这是您当前的查询:
SELECT start_break,
finish_break
FROM break_time
WHERE start_break AND finish_break BETWEEN '08:00:00' AND '17:00:00'
WHERE
子句表示start_break
评估为真,finish_break
为上午8点至下午5点。这可能不是您想要的,因为start_break
将始终评估为true。来自MySQL documentation:
MySQL将任何非零非NULL值评估为TRUE
除了WHERE
子句中的逻辑问题之外,您还尝试直接将开始列和结束列与仅限时间的字符串进行比较。除非这些列也是时间,否则这将无效。我怀疑。
以下是您可能逻辑上想要的查询:
SELECT start_break,
finish_break
FROM break_time
WHERE DATE_FORMAT(start_break, '%H:%i:%s') BETWEEN '08:00:00' AND '17:00:00' AND -- both the start break and
DATE_FORMAT(finish_break, '%H:%i:%s') BETWEEN '08:00:00' AND '17:00:00' -- finish break are within range
这假定start_break
和end_break
列为datetime
,您只想比较一天中的时间。