tags:

views:

23

answers:

2

The following script issues a 'Warning: range() [function.range]: step exceeds the specified range in' only when the date_diff function is called. Does anyone know why?

<?php

$array  =   array(
    "Interno",
    "id"
);

$step = count($array) - 1;

foreach (range(0, $step) as $number) {
    echo '<p>'.$number.'</p>';
}

$datetime1 = new DateTime('2010-08-2');
$datetime2 = new DateTime('2009-07-30');

$interval = date_diff($datetime1,$datetime2);
?>
A: 

Well, the two functions have nothing to do with each other.

Secondly, the second parameter to range is not a step, it's a maximum value (see the range docs... So if you're getting a step exceeds the specified range error, I'd guess that the default step value 1 is larger than the max of the range (the result of count($array) - 1)... I'm not sure why that's happening in your code, but it's a start

ircmaxell
(count($array) -1) is 1, so range(0,1) should output 0 1. And it does so, but if I use the date_diff function in the same page, I get the warning and no output.
mquasar
My guess (and why I posted it) is that `$array` is being changed. Is what you posted the **exact** code being used that you're getting the error with? If not, can you please post the exact code, as I have a feeling something else is going on besides what you posted...
ircmaxell
Well, the issue was raised in a small cakephp application. But I isolated the 'troublesome' code and got the same results. Could it be a php 5.3.0 issue? To make it clear, yes, that's the exact code I'm getting the warning with.
mquasar
There are a whole host of [bugs in `5.3.0`](http://php.net/ChangeLog-5.php). You should upgrade to `5.3.3`... I've run it a bunch of times on my 5.3.2 install, and it works fine every time...
ircmaxell
ok, I'll try that. Thanks for now. Hope it works :)
mquasar
A: 

I do agree with ircmaxell, function range and date_diff are not related and do not interact in any way. The issue should be in your array which get altered in some way. Also, as for me, your example contain unnecessary operations like count and range and it could be shortened to this:

<?php

$array  =   array(
    "Interno",
    "id"
);

foreach ($array as $number => $value) {
    echo '<p>'.$number.'</p>';
}

$datetime1 = new DateTime('2010-08-2');

$datetime2 = new DateTime('2009-07-30');

$interval = date_diff($datetime1,$datetime2);
?>
Nazariy
Well, yes. The reason I put it like that is because that's part of the cake library. I tried isolating the code to check if it was a cake problem or a php problem. And for some reason I still code the warning message in the isolated code.
mquasar