Jump to content

Archived

This topic is now archived and is closed to further replies.

shareen

Error returning date and time difference

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]

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
Hi,

yes thats the error message returned.

Do you know why its returning this and not the difference in seconds please?

Share this post


Link to post
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...


Share this post


Link to post
Share on other sites
Hi the problem is printing out the number of seconds.

I would just like to do a simple difference find between the two values.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Hi, sorry my fault.

The problem was always printing out the seconds.

I would just like to get the number of seconds between the two values.

Share this post


Link to post
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?

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.