shareen Posted April 27, 2006 Share Posted April 27, 2006 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] Quote Link to comment Share on other sites More sharing options...
Guest askjames01 Posted April 27, 2006 Share Posted April 27, 2006 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] Quote Link to comment Share on other sites More sharing options...
shareen Posted April 27, 2006 Author Share Posted April 27, 2006 Hi,yes thats the error message returned.Do you know why its returning this and not the difference in seconds please? Quote Link to comment Share on other sites More sharing options...
Guest askjames01 Posted April 27, 2006 Share Posted April 27, 2006 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... Quote Link to comment Share on other sites More sharing options...
shareen Posted April 27, 2006 Author Share Posted April 27, 2006 Hi the problem is printing out the number of seconds.I would just like to do a simple difference find between the two values. Quote Link to comment Share on other sites More sharing options...
Guest askjames01 Posted April 27, 2006 Share Posted April 27, 2006 did we solved the first problem "the error message"?is that the second problem the difference value?please keep posting. Quote Link to comment Share on other sites More sharing options...
shareen Posted April 27, 2006 Author Share Posted April 27, 2006 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. Quote Link to comment Share on other sites More sharing options...
Guest askjames01 Posted April 27, 2006 Share Posted April 27, 2006 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? Quote Link to comment Share on other sites More sharing options...
shareen Posted April 27, 2006 Author Share Posted April 27, 2006 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 answerThis 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. Quote Link to comment Share on other sites More sharing options...
shareen Posted April 28, 2006 Author Share Posted April 28, 2006 Hi, i have used the link and its not giving me any value.Any clues why?This is the date from the database28-04-06 16.52.51Then 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.