douque9815 2015-04-17 09:46
浏览 24
已采纳

从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?

  • 写回答

1条回答 默认 最新

  • dongluan1743 2015-04-21 07:46
    关注

    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');
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端