d.shankar Posted December 21, 2007 Share Posted December 21, 2007 I have these timestamps but i am unable to add it. 00:16:45 00:22:12 01:08:04 00:23:13 00:40:17 00:35:22 00:28:11 00:19:48 00:32:19 00:00:00 00:50:18 01:42:08 Is there any specific function ? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 21, 2007 Share Posted December 21, 2007 add it where, please elaborate ? Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 21, 2007 Author Share Posted December 21, 2007 I need to find the total time using PHP. Did you get it ? Quote Link to comment Share on other sites More sharing options...
chigley Posted December 21, 2007 Share Posted December 21, 2007 Use strtotime() to convert them all to seconds Add them up You have the total time in seconds Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 21, 2007 Author Share Posted December 21, 2007 I am getting in microseconds how shall i convert it ? 1198196205 Seconds 1198196532 Seconds 1198199284 Seconds 1198196593 Seconds 1198197617 Seconds 1198197322 Seconds 1198196891 Seconds 1198196388 Seconds 1198197139 Seconds 1198195200 Seconds 1198198218 Seconds 1198201328 Seconds Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 21, 2007 Author Share Posted December 21, 2007 Bump 1 Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 21, 2007 Share Posted December 21, 2007 what you want to convert it into ? Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 21, 2007 Author Share Posted December 21, 2007 I want to convert each of the time to seconds , so that i can add up all seconds and find the total time. Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 21, 2007 Share Posted December 21, 2007 The strtotime() suggestion was incorrect. That results in a UNIX timestamp, which is not the number of seconds the HH:MM:SS represents and adding them would have no meaning. You need to break apart (explode) or parse (preg_match) the HH:MM:SS and then do the simple math to convert the HH and MM to seconds and add all three fields. This will give the number of seconds. Then simply add all the values. When you get done, just do the division/subtraction to convert seconds back to HH:MM:SS. Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 21, 2007 Author Share Posted December 21, 2007 Hmm yea u are right dude.. I was trying the strtotime for a long time .. it didnt help at all !. isn't there any function to do this ?? Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted December 21, 2007 Share Posted December 21, 2007 here you go <?php $strHours = "16:59:45"; echo fnHoursToSeconds($strHours)."<BR>"; echo fnSecondsToHours(fnHoursToSeconds($strHours))."<BR>"; function fnHoursToSeconds($strHours) { list($intHours,$intMinutes,$intSeconds) = explode(":",$strHours); return (($intHours*3600)+($intMinutes*60)+$intSeconds); } function fnSecondsToHours($intSeconds) { return (floor($intSeconds/3600)).":".(floor($intSeconds/60)%60).":".($intSeconds%60); } ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 21, 2007 Share Posted December 21, 2007 Slightly different but the same results - <?php $times = array(); $times[] = "00:16:45"; $times[] = "00:22:12"; $times[] = "01:08:04"; $times[] = "00:23:13"; $times[] = "00:40:17"; $times[] = "00:35:22"; $times[] = "00:28:11"; $times[] = "00:19:48"; $times[] = "00:32:19"; $times[] = "00:00:00"; $times[] = "00:50:18"; $times[] = "01:42:08"; $seconds = 0; foreach($times as $time) { $parts = explode(":",$time); $seconds += ($parts[0] * 3600) + ($parts[1] * 60) + $parts[2]; } echo "Total sec: $seconds<br />"; // quick function found by searching the Internet to convert to HH:MM:SS function sec2hms ($sec) { $hms = ""; $hours = intval(intval($sec) / 3600); $hms .= str_pad($hours, 2, "0", STR_PAD_LEFT). ':'; $minutes = intval(($sec / 60) % 60); $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT). ':'; $seconds = intval($sec % 60); $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT); return $hms; } echo "HH:MM:SS " . sec2hms($seconds) . "<br />"; ?> Quote Link to comment Share on other sites More sharing options...
d.shankar Posted December 21, 2007 Author Share Posted December 21, 2007 Thank you guys you both rock !!!!!!!!!! Quote Link to comment Share on other sites More sharing options...
chigley Posted December 21, 2007 Share Posted December 21, 2007 Which idiot suggested strtotime()? Sorry! 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.