var_dump changes the result of the function?
I'm on a script which is going to make automatic appointment and the snippet below is for finding an available time. However, I've come across something that i've never h开发者_如何学运维eard of before.
When i use var_dump in my script to see the variables(i always do that when coding) the scripts run OK. However, when i remove these lines, script stops working as intended. as i cannot use var_dump for debugging since it makes the script running.
function getfirstfree($length, $limit) {
$length = new DateInterval('PT' . $length . 'M');
$table = "randevu";
$today = date('Y-m-d', time());
$q = $this->db->get_where('randevu', array('date' => $today));
$events = $q->result_array();
if ($q->num_rows() > 0) {
for ($i=0; $i < sizeof($q); $i += 1) {
$c_ends = new DateTime($events[$i]['ends']);
$n_starts = new DateTime($events[$i+1]['starts']);
$freetime = $c_ends->diff($n_starts);
$length_w_break = $length;
$length_w_break->i = $length_w_break->i + 10;
var_dump($length_w_break);
var_dump($freetime);
if ($freetime > $length_w_break) {
echo "ok";
return $c_ends->add(new DateInterval('PT10M'));
}
else {
return "no";
}
}
}
}
without var_dump the scripts returns no. with var_dump, it returns the first free slot.
current values taken from the database is 1-> starts 12:56:09 ends 12:56:09 2-> starts 13:33:11 ends 13:33:11update var_dump($freetime); changes the output
In current PHP versions, DateInverval object and calculations with DateTime objects cause this bug. Avoid using them and use old-style, calculation with unix-time methods.
精彩评论