Jump to content


Photo

Error returning date and time difference


  • Please log in to reply
9 replies to this topic

#1 shareen

shareen
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 April 2006 - 05:23 PM

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
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 );
}

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


#2 Guest_askjames01_*

Guest_askjames01_*
  • Guests

Posted 27 April 2006 - 05:39 PM

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

Your probably forgot to define "second variable",

how about this codes:
if( $diff=@get_time_difference($starttime, $endtime) )
{
  echo "Hours: " .
       sprintf( '%02d:%02d:%02d', $diff['hours'], $diff['minutes'] ), $diff['seconds'] );
}
else
{
  echo "Hours: Error";
}


#3 shareen

shareen
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 April 2006 - 05:44 PM

Hi,

yes thats the error message returned.

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

#4 Guest_askjames01_*

Guest_askjames01_*
  • Guests

Posted 27 April 2006 - 05:56 PM

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

try this;

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

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




#5 shareen

shareen
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 April 2006 - 06:04 PM

Hi the problem is printing out the number of seconds.

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


#6 Guest_askjames01_*

Guest_askjames01_*
  • Guests

Posted 27 April 2006 - 06:08 PM

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

is that the second problem the difference value?

please keep posting.

#7 shareen

shareen
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 April 2006 - 06:10 PM

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.


#8 Guest_askjames01_*

Guest_askjames01_*
  • Guests

Posted 27 April 2006 - 06:22 PM

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?

#9 shareen

shareen
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 27 April 2006 - 07:03 PM

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?

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

echo $seconds_dif;

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.



#10 shareen

shareen
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 28 April 2006 - 03:55 PM

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

$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);

Please help, this is getting very awkward.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users