math operations on time variables

if i have 2 time variables:

    a = 00:00:12 and b = 00:00:05

how would i add them together to make :

    c = 00:00:17 ?

then i need to divide them to crate an average, but im stuck on the add part.

i get the data from the database in this format and when i try a simple :

    c=a+b;

i get :

    00

how do i implement simple math operations on a time variable?

Answers


Or you simply go straight forward ;-)

<?php
$a = "00:00:12";
$b = "00:00:05";

function addTime($timeA, $timeB) {
    $timeAcomponents = explode(":", $timeA);
    $timeBcomponents = explode(":", $timeB);

    $timeAinSeconds = $timeAcomponents[0]*60*60 + $timeAcomponents[1]*60 + $timeAcomponents[2];
    $timeBinSeconds = $timeBcomponents[0]*60*60 + $timeBcomponents[1]*60 + $timeBcomponents[2];

    $timeABinSeconds = $timeAinSeconds + $timeBinSeconds;

    $timeABsec = $timeABinSeconds % 60;
    $timeABmin = (($timeABinSeconds - $timeABsec) / 60) % 60;
    $timeABh = ($timeABinSeconds - $timeABsec - $timeABmin*60) / 60 / 60;

    return str_pad((int) $timeABh,2,"0",STR_PAD_LEFT).":"
          .str_pad((int) $timeABmin,2,"0",STR_PAD_LEFT).":"
          .str_pad((int) $timeABsec,2,"0",STR_PAD_LEFT);
}

echo "Adding time variables:\n";
echo "$a + $b = ".addTime($a, $b);
?>

$date['first'] = DateTime::createFromFormat('H:i:s', "00:00:12");
$date['second'] = DateTime::createFromFormat('H:i:s', "00:00:05");

$interval = new DateInterval('PT'. $date['second']->format('s') .'S');

$date['first']->add($interval);

echo $date['first']->format('s'); // echoes 17

Just use mktime for all operations and then convert it back to readable format (using date) when you output dates:

$a = mktime(0, 0, 12);
$b = mktime(0, 0, 5);

echo date('G:i:s', $a + $b);
echo date('G:i:s', ($a + $b) / 2);

In MySQL you can do it in this way -

SET @a = '00:00:12';
SET @b = '00:00:05';

SET @a_sec = TIME_TO_SEC(@a);
SET @b_sec = TIME_TO_SEC(@b);

SET @c_sec = @a_sec + @b_sec;

SELECT SEC_TO_TIME(@c_sec);
+---------------------+
| SEC_TO_TIME(@c_sec) |
+---------------------+
| 00:00:17            |
+---------------------+

Need Your Help

Have I completed this C++ pointers / lists assignment?

c++ pointers list code-review

Write a class ListNode which has the following properties: int value; ListNode *next; Provide the following functions: ListNode(int v, ListNode *l) int getValue();

Getting the Max Value of One-To-Many a relation in CoreData through NSPredicatae

ios core-data max nspredicate database-relations

I'm creating a timeclock application where there is a ClockPunch NSManagedObject model with properties representing a clockin and clockout punch. Each ClockPunch instance has a relationship with an

How to prevent jar decompilation

java jar obfuscation

I have created a plugin project in Eclipse, distributed as a jar.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.