0

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 heard 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:11

update var_dump($freetime); changes the output

Umur Kontacı
  • 35,403
  • 8
  • 73
  • 96
  • You give a whole lot of info, but what about the most important: *what is `var_dump` printing*? – Jon Jul 04 '11 at 12:14
  • 4
    `var_dump($freetime); changes the output` --- it is not possible. I bet a beer. – zerkms Jul 04 '11 at 12:14
  • @Jon _it prints_ `object (DateInterval)#20 (8) { ["y"]=> int(0) ["m"]=> int(0) ["d"]=> int(0) ["h"]=> int(0) ["i"]=> int(37) ["s"]=> int(2) ["invert"]=> int(0) ["days"]=> int(6015) }` – Umur Kontacı Jul 04 '11 at 12:17
  • `var_dump()` does only output information about a variable and it's content, it does not change the variable dumped. So it can only change the output but not the program flow within your function. Please specify how you determine that you function runs as intended or not. – hakre Jul 04 '11 at 12:17
  • @hakre, i also know it is. it is something strange. i call my function that i need 20minutes of free time for an appointment `getfirstfree($length)` (2nd parameter is not considered atm). there is a space for in my table, and when i var_dump, the script and the _if_ statement works well, returns the `return $c_ends->add(new DateInterval('PT10M'));`. when i remove var_dump it goes to _else_ statements and returns `"no"` – Umur Kontacı Jul 04 '11 at 12:22
  • it also does the same thing with `print_r($freetime)` – Umur Kontacı Jul 04 '11 at 12:23
  • just place a `echo "hello world.";` instead of `var_dump` or `print_r`. Is the echo needed as well to make the function work as intended? If so, just make `echo '';` and try again. If this still works you've found a work-around ;) – hakre Jul 04 '11 at 12:37
  • @fastreload - I see that the object has a *days* of 6015. See http://acme-tech.net/blog/2010/10/12/php-datetimediff-returns-6015/ which seems to indicate that this value indicates a known windows DateTime bug. – borrible Jul 04 '11 at 12:41
  • If I understand correctly your var_dump variables are objects. Hence, the question would not be why var_dump changes the result but why the object change value when they are called. – Adrian World Jul 04 '11 at 13:49
  • @hakre, no it doesn't work. @borrible, i guess that's the issue, hence i'm postponing scripting this until i upload to my server. @adrian, if they were objects, then they should change when they are called in the if statement, afaik? – Umur Kontacı Jul 04 '11 at 18:58
  • update folks: i have moved my server to my real host, linux, ubuntu, apache2, php 5.3, however this is ongoing, yet days(6015) bug has been gone. i use var_dump, it functions properly, i don't, i fails... – Umur Kontacı Jul 08 '11 at 12:09

1 Answers1

0

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.

Umur Kontacı
  • 35,403
  • 8
  • 73
  • 96