Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts

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

    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:
    PHP Code:
    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:
    PHP Code:
    ### 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
    Last edited by mOrloff; 11-24-2010 at 10:39 PM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    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')));

  • #3
    New to the CF scene
    Join Date
    Nov 2010
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

    PHP Code:
    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.

  • #4
    Regular Coder
    Join Date
    Sep 2010
    Location
    Virginia
    Posts
    112
    Thanks
    11
    Thanked 7 Times in 7 Posts
    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:

    PHP Code:
    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;

    Last edited by Afro_Programmer; 11-25-2010 at 02:43 AM.

  • #5
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Oh my goodness!
    I can't believe that I just put the equation in the wrong order

    Thanks AfroP, for catching that.
    ~ Mo

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    just as an aside , ignoring the extrayear bit which I dont get ... is this not easier ?

    PHP Code:
    echo strtotime('21:01:02') - strtotime('20:01:02'); //  3600
    echo strtotime('21:01:02') - strtotime('22:01:02'); // -3600 
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •