【问题标题】:php average of timestampphp平均时间戳
【发布时间】:2013-08-27 13:57:22
【问题描述】:

我想计算用户在特定持续时间内的平均时间,我有每个时间的时间戳值。 要计算平均值,我想添加所有时间戳并除以天数。 但是所有时间戳的总和给出了错误的输入,所以我想将时间戳转换为秒,这样我就可以添加它们并计算平均值。 我正在使用以下代码。

$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 中有什么内容?如果它是TIMESTAMP 或者当前是DATE,它不是已经在几秒钟内了吗?
  • 这是一个TIMESTAMP,但如果直接使用时间戳进行平均,它会给我错误的结果。
  • hmm,时间戳单位是秒,将时间戳转换为秒,这只是无意义的......请更改您的问题标题!
  • @SandipPingle 如果我错了,请纠正我。打卡时间是打卡时间?一个用户有多个时间?例如,我们有一个用户正在跑步,并且每公里记录他的时间;所以你需要他 1 公里所需的平均时间?
  • A timestamp 是一个整数,表示自 1970 年 1 月 1 日以来经过的秒数。您需要什么?

标签: php datetime timestamp average


【解决方案1】:

您的问题不是很清楚,但我想我理解您想从一系列打卡时间中计算平均打卡时间。

日期对此没有好处,您需要隔离每个$punchintime 午夜后的秒数并计算其平均值。下面的代码就是这样做的。我创建了一个时间数组来说明我的观点,我对你的系统一无所知,所以生成输入数组取决于你。

$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);

输出:-

平均时钟时间为 09h 00m 10s

这不适用于跨越午夜的时间,例如这样的数组:-

$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. 四年过去了,你还没找到时间诶 :-D
  • @Abela 显然不是 :)
【解决方案2】:

你说的是unixtime。 Unixtime 是从 unix 纪元(1970 年 1 月 1 日)开始的秒数。要使两个时间戳的时间不同,您只需从第二个时间戳中减去第一个时间戳即可。

$timestamp1 = date('U');

然后过了一段时间:

$timestamp2 = date('U');

存储这些变量以及何时获取差异:

$difference = $timestamp2 - $timestamp1;

然后您可以使用基本数学来格式化时间:

$seconds = $difference;
$minutes = $seconds/60; 
$hours = $minutes/60;
$days = $hours/24;

希望这会有所帮助!

【讨论】:

    【解决方案3】:

    试试这个代码

    public static function sec2hms($sec, $padHours = false) {
    
        // start with a blank string
        $hms = "";
    
        // do the hours first: there are 3600 seconds in an hour, so if we divide
        // the total number of seconds by 3600 and throw away the remainder, we're
        // left with the number of hours in those seconds
        $hours = intval(intval($sec) / 3600);
    
        // add hours to $hms (with a leading 0 if asked for)
        $hms .= ($padHours) ? str_pad($hours, 2, "0", STR_PAD_LEFT) . ":" : $hours . ":";
    
        // dividing the total seconds by 60 will give us the number of minutes
        // in total, but we're interested in *minutes past the hour* and to get
        // this, we have to divide by 60 again and then use the remainder
        $minutes = intval(($sec / 60) % 60);
    
        // add minutes to $hms (with a leading 0 if needed)
        $hms .= str_pad($minutes, 2, "0", STR_PAD_LEFT) . ":";
    
        // seconds past the minute are found by dividing the total number of seconds
        // by 60 and using the remainder
        $seconds = intval($sec % 60);
    
        // add seconds to $hms (with a leading 0 if needed)
        $hms .= str_pad($seconds, 2, "0", STR_PAD_LEFT);
    
        // done!
        return $hms;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多