开发者

checking database for necessary updates using dateDiff

hey stackoverflowers. im having a few problems with the code i have written to check my database on refresh if all record dates on the database are between the specified $min and $max values. if not then the record will be updated. this is what i have done so far.

// 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);
    $datetim开发者_Python百科e_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());    
    }
    }

the logic behind it seems rather rational but whenever i try to run the code i get the error message:

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

any idea whats going on?


  1. fetching into $row but using $row_update
  2. UPDATE rent SET colour='F0F0F0' will update ALL records if one tupel is not between $min and $max
  3. Why not UPDATE rent SET colour='F0F0F0' WHERE pDate NOT BETWEEN $min AND $max MySQL Between


I think your approach is not optimized, instead of compare the different from php->mysql->php, you can issue a query to directly update to mysql, like

update rent set colour='F0F0F0' WHERE pDate>='$min' and pDate<='$max';
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜