Sum time from datetime

I have an table in sql the type is datetime y m d h i s and im using mid in php to get the time alone and the date alone like this mid(tablename,1,10) to get the date only like this 'y m d' and useing mid(tablename,12,8) to get the time only like this 'H:i:s' and this is code explane what i did

<html> 
<head> 
<title>ss</title> 
</head> 
<body> 
    <form method='post' action='<?php $_SERVER['SELF'];?>'> 
        <input type='submit' name='submit' /> 
    </form> 
    <? 
        include("config"); 
        $submit = $_POST['submit']; 
        $getdata1= mysql_query("select checktime from calls"); 
        if(isset($submit)) 
        { 
            while($getdata= mysql_fetch_array($getdata1)) 
            { 
                echo "<input type='text' name='text' value='mid($getdata[1],12,8)'";
        } 
    }  
?> 
</body> 
</html>

now I get the result like this

08:00:00
08:10:00
07:00:51

and so on What I need to sum the result as

date("d H:i:s",strtotime(all result from fetch array))

so I tried this


<html> 
<head> 
<title>ss</title> 
</head> 
<body> 
    <form method='post' action='<?php $_SERVER['SELF'];?>'> 
        <input type='submit' name='submit' /> 
    </form> 
    <? 
        include("config"); 
        $submit = $_POST['submit']; 
        $getdata1= mysql_query("select checktime from calls"); 
        if(isset($submit)) 
        {  
            while($getdata= mysql_fetch_array($getdata1)) 
            { 
                echo "<input type='text' name='text' value='mid($getdata[1],12,8)'";
                for($i=0;$i<5;$i++)
                {
                    $i+= strtotime($getdata[0]);
                }
            } 
        echo $i;
    } 
?> 
</body> 
</html>

but I have only the last result

so I tried to do it in sql query like this

<html> 
<head> 
<title>ss</title> 
</head> 
<body> 
    <form method='post' action='<?php $_SERVER['SELF'];?>'> 
        <input type='submit' name='submit' /> 
    </form> 
    <? 
    include("config"); 
    $submit = $_POST['submit']; 
    $getdata1= mysql_query("select unix_timestamp(mid(checktime,12,8)) from calls"); 
    if(isset($submit)) 
    { 
        while($getdata= mysql_fetch_array($getdata1)) 
        { 
            echo "<input type='text' name='text' value='mid($getdata[1],12,8)'";
            for($i=0;$i<5;$i++)
            {
                $i+= $getdata[0];
            }
        } 
        echo $i;
    } 
?> 
</body> 
</html>

and the result I get at not from 1970-1-1, but I get at from mid(checktime,1,10), so what I want is to sum the result thats comes out like this: H:i:s from the whole fetch.

I mean, how can I sum the result to come out like this: 08:00:00 08:10:00 07:00:51 in one variable?

Upvotes: 0

Views: 207

Answers (1)

kba
kba

Reputation: 4310

If I understand well to you question, here is small example how you can sum up and format time as you required.

$times = ['15:00:00', '08:10:00', '07:00:51'];

$date = new \DateTime;
$alteredDate = new \DateTime;

foreach ($times as $time) {
    $parts = explode(':', $time);
    $alteredDate->add(new \DateInterval(sprintf('PT%dH%dM%dS', $parts[0], $parts[1], $parts[2])));
}

echo $alteredDate->diff($date)->format('%a %h:%i:%s');

Upvotes: 1

Related Questions