Jump to content

[SOLVED] Adding Time


d.shankar

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.