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 ? Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/ Share on other sites More sharing options...
rajivgonsalves Posted December 21, 2007 Share Posted December 21, 2007 add it where, please elaborate ? Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420359 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 ? Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420360 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 Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420362 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 Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420396 Share on other sites More sharing options...
d.shankar Posted December 21, 2007 Author Share Posted December 21, 2007 Bump 1 Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420424 Share on other sites More sharing options...
rajivgonsalves Posted December 21, 2007 Share Posted December 21, 2007 what you want to convert it into ? Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420426 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. Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420427 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. Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420428 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 ?? Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420429 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); } ?> Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420433 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 />"; ?> Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420436 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 !!!!!!!!!! Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420543 Share on other sites More sharing options...
chigley Posted December 21, 2007 Share Posted December 21, 2007 Which idiot suggested strtotime()? Sorry! Link to comment https://forums.phpfreaks.com/topic/82653-solved-adding-time/#findComment-420544 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.