// check database for necessary updates
$update = mysql_query("SELECT * FROM rent");
while($row = mysql_fetch_array( $update )) {
$datetime_lower = DateTime::createFromFormat('d/m/Y', $min);
$datetime_upper = DateTime::createFromFormat('d/m/Y', $max);
$datetime_compare = DateTime::createFromFormat('d/m/Y g:i a', $row_update['pDate']);
$diff_lower = $datetime_lower->diff($datetime_compare);
$diff_upper = $datetime_upper->diff($datetime_compare);
if ($datetime_lower < $datetime_compare && $datetime_upper > $datetime_compare) {
// date is between min and max, do nothing
} else {
// date is not between min and max, update cell colour
$result = mysql_query("UPDATE rent SET colour='F0F0F0' WHERE $datetime_lower < $pDate && $datetime_upper > $pDate") or die(mysql_error());
}
}
它背后的逻辑似乎相当理性,但每当我尝试运行代码时,我都会收到错误消息:
Warning: DateTime::diff() expects parameter 1 to be DateTime, boolean given in C:\xampp\htdocs\keypad\main.php on line 41
Warning: DateTime::diff() expects parameter 1 to be DateTime, boolean given in C:\xampp\htdocs\keypad\main.php on line 42
有什么想法吗?
答案 0 :(得分:1)
$row
$row_update
如果一个图案不在$ min和$ max 之间,UPDATE rent SET colour='F0F0F0'
将更新所有记录
UPDATE rent SET colour='F0F0F0' WHERE pDate NOT BETWEEN $min AND $max
MySQL Between 答案 1 :(得分:1)
我认为你的方法没有优化,而不是比较php->mysql->php
的不同,你可以发出一个查询来直接更新到mysql,比如
update rent set colour='F0F0F0' WHERE pDate>='$min' and pDate<='$max';