Solved

Is there something wrong with this PHP function?

Posted on 2013-02-05
9
347 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
ID: 38857380
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 109

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 38858056
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
ID: 38859490
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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 27

Accepted Solution

by:
yodercm earned 250 total points
ID: 38859949
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
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38863633
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
ID: 38864116
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
ID: 38865080
>> 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
ID: 38865678
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 109

Expert Comment

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

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

820 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