Link to home
Start Free TrialLog in
Avatar of egoselfaxis
egoselfaxis

asked on

Is there something wrong with this PHP function?

I have the following PHP function in place on an eBay sniping website that I manage:
 
function get_time_difference( $start, $end ) {
    $uts['start']      =    strtotime( $start );
    $uts['end']        =    strtotime( $end );
    if( $uts['start']!==-1 && $uts['end']!==-1 )
    {
        if( $uts['end']>= $uts['start'] )
        {
            $diff    =    $uts['end'] - $uts['start'];
            if( $days=intval((floor($diff/86400))) )
                $diff = $diff % 86400;
            if( $hours=intval((floor($diff/3600))) )
                $diff = $diff % 3600;
            if( $minutes=intval((floor($diff/60))) )
                $diff = $diff % 60;
            $diff    =    intval( $diff ); 
			if($days>0 || $hours >0 || $minutes>0 || $diff>0)
            return( array('days'=>$days, 'hours'=>$hours, 'minutes'=>$minutes, 'seconds'=>$diff) );
        }
        else
        {
           trigger_error( "Ending date/time is earlier than the start date/time", E_USER_WARNING );
        }
    }
    else
    {
        trigger_error( "Invalid date/time data detected", E_USER_WARNING );
    }
    return( false );
}

Open in new window


I suspect strongly that something is wrong with this function which makes it sometimes result in incorrect calculations, and I was wondering if someone here might be able to take a glance at it and verify if my hunch is correct.  Also, .. if something is indeed wrong with it, I'd love to know what the appropriate fixes would be.  

By the way -- the expected date format for the start & end dates is as follows:

date("Y-m-d h:i:s");

Open in new window

     

Thanks!
- Yvan
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

Instead of asking someone to peruse your entire function to guess what it is supposed to do, please give some specific examples of what you input, what is supposed to happen, and what actually happens that is incorrect.
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of egoselfaxis
egoselfaxis

ASKER

I'm not sure why my question is being scrutinized. Ray -- you're always very helpful to me here on Experts Exchange.  And you were able to simply look at my PHP function and to determine exactly what it's supposed to do ... which is exactly what I expect from any individual who replies to my question.  

We can make certain assumptions regarding the the start & end dates that are being passed to the function -- that they're in the correct date format (as I've indicated) and that the end date will always be greater than the start date, etc.  I'd just like to know if there's anything glaringly obvious that would be resulting in the wrong calculations.

For example, .. when I expect to see a time such as  17d 9h 56m 46s ... I'll instead see a time such as  16d 21h 56m 46s.  Why is that?  

- Yvan
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Please create the SSCCE that will give us the test data, along with the expected output.  This is not meant to insult or encumber you; it's just that I am teaching full time now and I do not have time to create test data cases for failing code.  If I can see test data cases and expected results, I probably have time to write the code.  And I have a room full of curious students who would gladly help with the code once the problem definition is clear.
Umm, Ray, did you notice that I already gave him the answer to what is wrong?  He's using 12 hour format for the input, so 11am and 11pm look like 11am to the function.
>> Umm, Ray, did you notice that I already gave him the answer to what is wrong?  He's using 12 hour format for the input, so 11am and 11pm look like 11am to the function.

Thank you Yodercm --- you were 100% correct.  The problem was resulting from my using 12 hour format instead of 24 hour format.   I'm not entirely grasping why this turned out to be problematic --- but I'm sure it'll come to me :)

By the way ... I wasn't trying to insult anyone with any of my replies. I stare at code like this all day long almost every single day and have to figure out similar problems, .. and I just got fatigued is all.  I asked for help ..  was happy to provide additional information/examples when asked, .. and now I am generously awarding thank you points to all who where involved :).

Thanks guys!
- Yvan
So just out of curiosity, why did you give half the points to Ray, who didn't solve your problem and didn't contribute anything useful, other than to say he agreed with me.
Thanks for the points -- hope the articles were useful, ~Ray