Solved

Is there something wrong with this PHP function?

Posted on 2013-02-05
9
340 Views
Last Modified: 2013-02-08
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
0
Comment
Question by:egoselfaxis
  • 4
  • 3
  • 2
9 Comments
 
LVL 27

Expert Comment

by:yodercm
Comment Utility
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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
 

Author Comment

by:egoselfaxis
Comment Utility
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
 
LVL 27

Accepted Solution

by:
yodercm earned 250 total points
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 
LVL 27

Expert Comment

by:yodercm
Comment Utility
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
 

Author Comment

by:egoselfaxis
Comment Utility
>> 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
 
LVL 27

Expert Comment

by:yodercm
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Thanks for the points -- hope the articles were useful, ~Ray
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Responsive Design Tools 6 34
echo button 13 45
comparing strings that contain line breaks 18 34
Instering to MySQL table 5 13
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now