Jump to content

Error returning date and time difference


shareen

Recommended Posts

Hi,

I am trying to calculate the number of seconds difference between two dates. The problem is this function keeps returing an error and i am definitley returning two time and dates with seconds included.

Does someone know where i am going wrong? The dates and times definitley have seconds.


This is the function
[code]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 );            
            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 );
}[/code]

This is the piece of code that returns the error or the difference.
[code]if( $diff=@get_time_difference($starttime, $endtime) )
{
  echo "Hours: " .
       sprintf( '%02d:%02d', $diff['hours'], $diff['minutes'] );
}
else
{
  echo "Hours: Error";
}[/code]
Link to comment
Share on other sites

Guest askjames01
Is this the error message --> Hours: Error , that your codes generating?

Your probably forgot to define "second variable",

how about this codes:
[code]
if( $diff=@get_time_difference($starttime, $endtime) )
{
  echo "Hours: " .
       sprintf( '%02d:%02d:%02d', $diff['hours'], $diff['minutes'] ), $diff['seconds'] );
}
else
{
  echo "Hours: Error";
}
[/code]
Link to comment
Share on other sites

Guest askjames01
ok, i get your problem now...
and i hope this will fix it.

try this;

[code]
if( $diff=@get_time_difference($starttime, $endtime) )
{
  echo "Hours: " .
       sprintf( '%02d:%02d', $diff['hours'], $diff['minutes'] );
}
elseif ("Your condition here")
{
  echo "Hours: Error";
}
[/code]

Please don't forget to put condition statement to your 'elseif',
this might solve your problem... If not then keep posting...


Link to comment
Share on other sites

Guest askjames01
did we solved the first problem "the error message"?

is that the second problem the difference value?

please keep posting.
Link to comment
Share on other sites

Guest askjames01
The following URL will give you an IDEA!

[a href=\"http://www.webhostingtalk.com/archive/thread/453668-1.html\" target=\"_blank\"]http://www.webhostingtalk.com/archive/thread/453668-1.html[/a]

Did i helped you?
Link to comment
Share on other sites

Hi, thanks for the link.

Almost there i think.

The value i want to compare it to is the database value.

It gives me a really really high number, that is no where near the true value.

Any ideas?

[code]$seconds_dif = mktime($starttime) - mktime(date("d"),date("m"),date("y"),date("G"),date("i"),date("s"));

echo $seconds_dif;[/code]

1146193195 This is the answer

This is no where near the ammount. I insert it and press it refresh and the difference should be a few seconds. I have no idea why.

Link to comment
Share on other sites

Hi, i have used the link and its not giving me any value.

Any clues why?

This is the date from the database
28-04-06 16.52.51

Then i am trying to take away the current time. Time now is 16:53:34

[code]$array = explode(" ",$starttime);
$array2 = explode("-",$array[0]);
$array3 = explode(".",$array[1]);

echo $starttime . "<br>";
$seconds_dif = mktime($array2[0],$array2[1],$array3[2],$array3[0],$array3[1],$array3[2]) - mktime(date("d"),date("m"),date("y"),date("G"),date("i"),date("s"));

echo "Seconds:" . floor($seconds_diff);[/code]

Please help, this is getting very awkward.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.