php时间戳的平均值 [英] php average of timestamp
问题描述
我想计算用户在特定持续时间内的平均时间,我有每个时间戳的时间戳值。
要计算平均值,我想添加所有时间戳,除以天数。
但是所有时间戳的总和给出错误的输入,所以我想将时间戳转换为秒,所以我可以添加它们并计算平均值。
我正在使用以下代码。
$ timeInTotalSec = 0;
$ timeInTotalSec + = intval(date(H,$ punchintime))* 60 * 60;
$ timeInTotalSec + = intval(date(i,$ punchintime))* 60;
$ timeInTotalSec + = intval(date(s,$ punchintime));`
但是
date(H,$ punchintime)
给我正确的值,但是
intval(date(H ,$ punchintime))
给我 0
提前感谢
你的问题不是很清楚,但我认为我了解你想从一连串的时间中计算一下平均时间。
日期是不好的,你需要隔离秒数午夜为每个 $ punchintime
,并计算它的平均值。以下代码这样做。我创建了一系列时间来说明我的观点,我不知道你的系统,所以生成输入数组是由你而定。
code> $ punchInTimes = array(
'2013-08-01 09:00',
'2013-08-02 09:06',
'2013-08-03 08:50',
'2013-08-04 09:20',
'2013-08-05 09:01',
'2013-08-06 08:56'
);
函数getAverageTime(array $ times)
{
$ seconds = $ average = 0;
$ result = null;
//午夜后得到
foreach($ times as $ dateString){
$ date = new \DateTime($ dateString);
list($ datePart)= explode('',$ dateString);
$ midnight = new \DateTime($ datePart);
$ seconds + = $ date-> getTimestamp() - $ midnight-> getTimestamp();
}
if($ seconds> 0){
$ average = $ seconds / count($ times);
$ hours = floor($ average / 3600);
$ average - =($ hours * 3600);
$ minutes = floor($ average / 60);
$ average - =($ minutes * 60);
$ result = new \DateInterval(PT {$ hours} H {$ minutes} M {$ average} S);
} else $ result = new \DateInterval('PT0S');
return $ result-> format(%Hh%Mm%Ss);
}
回声平均时钟是。 getAverageTime($ punchInTimes);
输出: -
平均时钟为09h 00m 10s
这不适用于跨越午夜,像这样的阵列: -
$ aroundMidnight = array(
'2013-08-01 23 :59',
'2013-08-02 00:02',
);
如果我稍后再回来,我会看看应对这种情况。 >
I want to calculate average of time-in for a user for particular duration , i have timestamp values for each time-in . To calculate average i want to add all timestamps and divide by no of days . But sum of all timestamps gives wrong input so i want convert timestamps to seconds so i can add them and calculate average . I am using following code .
$timeInTotalSec = 0;
$timeInTotalSec += intval(date("H",$punchintime)) * 60 * 60;
$timeInTotalSec += intval(date("i",$punchintime)) * 60;
$timeInTotalSec += intval(date("s",$punchintime));`
but
date("H",$punchintime)
gives me proper value but
intval(date("H",$punchintime))
giving me 0
Thanks in advance .
Your question isn't very clear, but I think I understand that you want to calculate the average punch in time from a series of punch in times.
Dates are no good for this, you need to isolate the number of seconds after midnight for each $punchintime
and calculate the average of that. The following code does that. I created an array of times to illustrate my point, I don't know anything about your system, so generating the input array is down to you.
$punchInTimes = array(
'2013-08-01 09:00',
'2013-08-02 09:06',
'2013-08-03 08:50',
'2013-08-04 09:20',
'2013-08-05 09:01',
'2013-08-06 08:56',
);
function getAverageTime(array $times)
{
$seconds = $average = 0;
$result = null;
//get seconds after midnight
foreach($times as $dateString){
$date = new \DateTime($dateString);
list($datePart) = explode(' ', $dateString);
$midnight = new \DateTime($datePart);
$seconds += $date->getTimestamp() - $midnight->getTimestamp();
}
if($seconds > 0){
$average = $seconds/count($times);
$hours = floor($average/3600);
$average -= ($hours * 3600);
$minutes = floor($average/60);
$average -= ($minutes * 60);
$result = new \DateInterval("PT{$hours}H{$minutes}M{$average}S");
} else $result = new \DateInterval('PT0S');
return $result->format("%Hh %Mm %Ss");
}
echo "Average clock in time is " . getAverageTime($punchInTimes);
Output:-
Average clock in time is 09h 00m 10s
This does not work for times that span midnight, such as an array like this:-
$aroundMidnight = array(
'2013-08-01 23:59',
'2013-08-02 00:02',
);
If I get time later on I'll have a look at coping with that case.
这篇关于php时间戳的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!