...

View Full Version : Why am I gettting a positive value (>0) when expecting a negative (<0) ??



mOrloff
11-24-2010, 11:36 PM
This function subtracts one time from another, and spits the difference back in seconds.
When I'm expecting a negative value, however, it returns it as a positive value.

Here's the func:


function getTimeDiff($beginTime,$endTime,$allowNegative=FALSE){
$begin=EXPLODE(':',$beginTime);
$begin=MKTIME($begin[0],$begin[1],$begin[2],DATE('n'),DATE('j'),DATE('y'));
$end=EXPLODE(':',$endTime);
if($allowNegative){
$diff=($begin - MKTIME($end[0],$end[1],$end[2],DATE('n'),DATE('j'),DATE('y')));
}else{
$extraDay=$beginTime>$endTime?1:0;
$diff=ABS($begin - MKTIME($end[0],$end[1],$end[2],DATE('n'),DATE('j')+$extraDay,DATE('y')));
}
return $diff;
}


And here's my test:


### this prints 75600 when I actually want -75600 ###
echo getTimeDiff("22:30:00","01:30:00",$allowNegative=TRUE)."<br/>\n";

### this prints 10800 as expected ###
echo getTimeDiff("22:30:00","01:30:00")."<br/>\n";


Why is it doing this, and how can I get my desired results??

Thanks-a-bunch,
~ Mo

mlseim
11-25-2010, 12:58 AM
ABS is "absolute". Which is the positive difference between two numbers ... always positive.


$diff=ABS($begin - MKTIME($end[0],$end[1],$end[2],DATE('n'),DATE('j')+$extraDay,DATE('y')));

ac04
11-25-2010, 01:29 AM
Having tested this myself, by echoing a string in each of the if/else blocks,

you stating that the Absolute affecting this is redundant. On the test that he's talking about, the first one, the following code is in use


if($allowNegative){
$diff=($begin - MKTIME($end[0],$end[1],$end[2],DATE('n'),DATE('j'),DATE('y')));
}

Identify for him where the abs() is in there.

If he's expecting a negative, which he should be, and he's getting a positive, even though there is no abs(), there's something wrong somewhere.

Afro_Programmer
11-25-2010, 03:00 AM
1,290,655,800 - 1,290,580,200 = 75,600

I don't get why you're expecting a negative. From what I can see, the function is doing what it's being told. No error...

-edit-

Here is what you want I guess:


function getTimeDiff($beginTime,$endTime,$allowNegative=FALSE){
$begin=EXPLODE(':',$beginTime);
$begin=MKTIME($begin[0],$begin[1],$begin[2],DATE('n'),DATE('j'),DATE('y'));
$end=EXPLODE(':',$endTime);
if($allowNegative){
$diff=(MKTIME($end[0],$end[1],$end[2],DATE('n'),DATE('j'),DATE('y')) - $begin); //that?
}else{
$extraDay=$beginTime>$endTime?1:0;
$diff=ABS(MKTIME($end[0],$end[1],$end[2],DATE('n'),DATE('j')+$extraDay,DATE('y')) - $begin);
}
return $diff;
}

mOrloff
11-29-2010, 05:13 PM
Oh my goodness!
I can't believe that I just put the equation in the wrong order :o

Thanks AfroP, for catching that.
~ Mo

firepages
11-30-2010, 11:10 AM
just as an aside , ignoring the extrayear bit which I dont get ... is this not easier ?



echo strtotime('21:01:02') - strtotime('20:01:02'); // 3600
echo strtotime('21:01:02') - strtotime('22:01:02'); // -3600



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum