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
egoselfaxisAsked:
Who is Participating?
 
Cornelia YoderConnect With a Mentor ArtistCommented:
In spite of your insult, I'll give you one hint.

In the format you gave

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

The lower case i means 12-hour format, as in AM and PM.  You most likely need to either convert it to upper case H (24-hour format), or figure out a way to provide the AM or PM part.

http://www.php.net/manual/en/function.date.php

This was trivial to figure out, once you gave some real information about the problem.
0
 
Cornelia YoderArtistCommented:
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.
0
 
Ray PaseurConnect With a Mentor Commented:
Hi, Yvan.  I am kind of with YoderCM on this one.  The function appears to look for some kind of elapsed time from one timestamp to another timestamp.  If the input data is out of whack (start greater than end, for example) you may get bizarre results.  So it may be data-dependent.  A better approach to this question might be to create the SSCCE and give us the test data, along with the expected output.  Armed with that information we can produce some useful code samples.

Info on processing DATETIME information:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html

Information on Test-Driven-Development:
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html

While the latter TDD example is not directly related to this question, it may help you design your thought processes around similar questions as you go forward.

Best regards, ~Ray
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
egoselfaxisAuthor Commented:
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
0
 
Ray PaseurCommented:
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.
0
 
Cornelia YoderArtistCommented:
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.
0
 
egoselfaxisAuthor Commented:
>> 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
0
 
Cornelia YoderArtistCommented:
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.
0
 
Ray PaseurCommented:
Thanks for the points -- hope the articles were useful, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.