如何从 PHP 中的时间戳列表中计算时间?

How to count time from list of timestamps in PHP?

我想计算在网站上花费的时间。我有每个用户的时间戳列表,每个时间戳之间的最小间隔至少为 60 秒。

编辑: 这是我的代码,不能按 100 秒的范围对它们进行分组

<?php
$numbers = array(1503541542,1503541602,1503541662,1503541722,1503541782,1503541842,1503541902,1503541962,1503542022,1503542082,1503542142,1503542202,1503542262,1503542322,1503542382,1503542442,1503542502,1503542562,1503542622,1503542682,1503542742,1503542798,1503542799,1503542859,1503542877,1503542878,1503542938,1503542961,1503542962,1503543022,1503543079,1503543080,1503543140,1503543200,1503543221,1503543221,1503543281,1503543341,1503543401,1503543461,1503543521,1503543581,1503543641,1503543701,1503543761,1503543821,1503543881,1503543941,1503544001,1503544061,1503544121,1503544181,1503544241,1503544301,1503544361,1503544421,1503544481,1503544541,1503544601,1503544661,1503544721,1503544781,1503544841,1503544901,1503545055,1503545056,1503545060,1503545061,1503545120,1503545173,1503545174,1503545181,1503545233,1503545240,1503545293,1503545301,1503545304,1503545304,1503545364,1503545424,1503545484,1503545544,1503545604,1503545664,1503545724,1503545784,1503545844,1503545904,1503545964,1503546024,1503546084,1503546144,1503546204,1503546264,1503546324,1503546358,1503546359,1503546419,1503546479,1503546539,1503546599,1503546659,1503546719,1503546779,1503546839,1503546899,1503546959,1503547019,1503547079,1503547139,1503547167,1503547167,1503547199,1503547218,1503547218,1503547254,1503547254,1503547259,1503547281,1503547282,1503547319,1503547340,1503547341,1503547379,1503547401,1503547439,1503547461,1503547499,1503547521,1503547559,1503547581,1503547619,1503547641,1503547679,1503547701,1503547739,1503547761,1503547799,1503547821,1503547859,1503547881,1503547919,1503547941,1503547979,1503548001,1503548039,1503548061,1503548099,1503548121,1503548159,1503548181,1503548219,1503548240);

// $numbers = array(10, 30, 230, 240, 250, 260);
$result = array();

while (count($numbers) > 0) {
    $begin = reset($numbers);
    $end = array_shift($numbers);

    while (in_array($end + 100, $numbers)){
        $end = array_shift($numbers);
    }

    $beginAndEnd = array_unique(array($begin, $end));
    $result[] = implode('-', $beginAndEnd);
}

print_r($result);
?>

提前致谢

在特定网站上获取用户时间戳的最佳方法是使用函数 "load" 和 "onunload",下面是代码:

  <!DOCTYPE html>
<html>
    <head>
        <title>Collect time</title>
        <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
        <script type="text/javascript">
        $(function()
        {
            var start = null;
            $(window).load(function(event) {
                start = event.timeStamp;
            });
            $(window).onunload(function(event) {
                var time = event.timeStamp - start;
                $.post('/collect-user-time/ajax-backend.php', {time: time});
            })
        });
        </script>
    </head>
    <body>

    </body>
</html>
And backend script:

<?php 
$time = intval($_POST['time']);
if (!file_exists('data.txt')) {
    file_put_contents('data.txt', $time . "\n");
} else {
    file_put_contents('data.txt', $time . "\n", FILE_APPEND);
}

嗯,你的问题不清楚,但如果我理解,这是我的想法:

  • 添加 2 个会话变量:
    • 1 var 用于在重新加载时注册旧页面并在重新加载时更新它
    • 1 var 用于注册时间戳并在重新加载时更新它

看这个:

if(isset($_SESSION['page']) && isset($_SESSION['timestamp'])){
    //REGISTER WHERE YOU WANT : BDD, LOGFILE...
    //$userStay=$db->prepare();
    //fopen('userStay', 'a+');

    //WITH THESE DATA
    $time = time() - $_SESSION['timestamp'];
    $page = $_SESSION['page'];
}
$_SESSION['page']=$_SERVER['REQUEST_URI'];
$_SESSION['timestamp']=time();