TalkPHP (http://www.talkphp.com/forums.php)
-   The Lounge (http://www.talkphp.com/lounge/)
-   -   On an interesting note (http://www.talkphp.com/lounge/4181-interesting-note.html)

 Enfernikus 04-23-2009 06:40 PM

On an interesting note

PHP Code:

``` abs(ceil((strtotime(\$endDate) - strtotime(\$startDate)) / 86400))  ```

Curiously, I got a negative number for this - PHP breaking the rules of math?

 Wildhoney 04-23-2009 06:45 PM

What do `\$endDate` and `\$startDate` equal? What makes you think it's breaking the rules of mathematics?

 Enfernikus 04-23-2009 07:17 PM

no matter what \$endDate and \$startDate equate to, abs() should make it positive. the absolute value of a negative is a positive.

 allworknoplay 04-23-2009 07:28 PM

When I do this, I get a positive number of 10.

\$startDate = "20 September 2000";
\$endDate = "10 September 2000";

\$test = abs(ceil((strtotime(\$endDate) - strtotime(\$startDate)) / 86400));

echo \$test;

 Village Idiot 04-23-2009 09:19 PM

Either your abs function is not working or something is happening to the data between the assigning and output. No real number can have a negative absolute value, an exception would have been thrown.

 sketchMedia 04-24-2009 11:54 AM

try to do it manually, see if it produces the same result, if not there may be somethings b0rked with abs, don't quite know what mind you, but just limit the possible avenues of error.

PHP Code:

``` function negToPos (\$i){    return \$i < 0 ? -\$i : \$i;}\$startDate = "20 September 2000";\$endDate = "10 September 2000";\$test = negToPos(ceil((strtotime(\$endDate) - strtotime(\$startDate)) / 86400));echo \$test;  ```
gives me, int 10 (as does the original abs() implementation)

 Salathe 04-24-2009 12:13 PM

To be frank, this thread is pointless unless it can be repeated. Does that code still return negative? What were the values of \$startDate and \$endDate which returned the negative value?

 All times are GMT. The time now is 05:50 AM.