从日期时间总和时间 [英] Sum time from datetime
问题描述
我有一个表中的SQL类型为datetime Y M深高I S
而在PHP中使用IM中旬拿到单独的时间,独自日期这样的中期(表名,1,10)
来获取日期只有这样'Y M D'
和期运用中期(表名,12,8)
来获取时间只有这样'H:I:S'
这是code explane我做了什么
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))
所以我想这个
<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
<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>
和结果我得到的不是从 1970年1
,但我得到从中期(checktime,1,10)
,所以我想是总结的结果,这就是出来是这样的: H:我:■
从整体取出。
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.
我的意思是,我怎么能总结的结果出来是这样的: 08:00:00
8时10分〇〇秒
7时00分51秒
在一个变量?
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?
推荐答案
如果我理解好想问你,这里是小例子,你需要,你如何总结和格式的时间。
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');
这篇关于从日期时间总和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!