0
function convertDates($timestamp) {
    return date('Y-m-d', $timestamp);
}
$days = array(); //storing databases php time();
$complete = array(); //storing the generated missing days
$occurrences = array(); //finding how many php time() are on same day
$zero = array(); //instead of storing occurrence as 1, store it as zero
$query = mysql_query("SELECT `login` FROM `statistics` ORDER BY `login` ASC");
while($rows = mysql_fetch_array($query)) {
    $days[] = $rows['login']; //filling array with times
}
$days[] = time(); //append current time
for($i = 0; $i < count($days); $i++) {
    $complete[] = convertDates($days[$i]);
    $difference = isset($days[$i+1]) ? $days[$i+1] - $days[$i] : 0;
    if($difference >= 86400) {
        $difference = ceil(abs($difference/86400));
        $fill = $days[$i];
        for($k = 0; $k < $difference; $k++) {
            $fill += 86400;
            $complete[] = convertDates($fill);
            $zero[] = convertDates($fill);
        }
    }
}
$occurrences = array_count_values($complete);
$complete = array_unique($complete);
$zero = array_unique($zero);
sort($complete);
echo "[";

for($i = 0; $i < count($zero); $i++) {
    echo "[\"".$zero[$i]."\",0], ";
}

for($i = 0; $i < count($occurrences); $i++) {
    if($i == count($occurrences)-1)
        echo "[\"".$complete[$i]."\",".$occurrences[$complete[$i]]."]";
    else {
        echo "[\"".$complete[$i]."\",".$occurrences[$complete[$i]]."], ";
    }
}
echo "]";

我正在将点绘制到 jqplot 图中,对于我需要定义用户未登录天数的统计数据。让我们称这些“差距”

用户于 2012 年 3 月 25 日登录,5 天后未重新登录。现在是 2012 年 3 月 30 日。我必须从这两个日期中生成那些缺失的日子。

我目前的算法似乎不起作用。我不知道出了什么问题。

数据库中的日期来自 phptime();

我将所有值存储到一个数组中$days

这是脚本生成的登录表的输出。请注意,它是为 jqplot 编码的:

[["2012-04-01",0], ["2012-04-02",0], ["2012-04-03",0], ["2012-04-04",0], ["2012-04-05",0], ["2012-04-07",0], ["2012-04-08",0], ["2012-04-09",0], ["2012-04-10",0], ["2012-04-11",0], ["2012-04-12",0], ["2012-04-13",0], ["2012-04-14",0], ["2012-04-15",0], ["2012-04-16",0], ["2012-04-17",0], ["2012-04-18",0], ["2012-04-19",0], ["2012-04-20",0], ["2012-04-21",0], ["2012-04-22",0], ["2012-04-23",0], ["2012-04-24",0], ["2012-04-25",0], ["2012-04-26",0], ["2012-04-27",0], ["2012-04-28",0], ["2012-04-29",0], ["2012-04-30",0], ["2012-03-31",1], ["2012-04-01",2], ["2012-04-02",2], ["2012-04-03",1], ["2012-04-04",19], ["2012-04-05",13], ["2012-04-06",8], ["2012-04-07",1], ["2012-04-08",1], ["2012-04-09",4], ["2012-04-10",1], ["2012-04-11",1], ["2012-04-12",2], ["2012-04-13",1], ["2012-04-14",1], ["2012-04-15",1], ["2012-04-16",2], ["2012-04-17",10], ["2012-04-18",1], ["2012-04-19",1], ["2012-04-20",1], ["2012-04-21",1], ["2012-04-22",1], ["2012-04-23",1], ["2012-04-24",1], ["2012-04-25",1], ["2012-04-26",1], ["2012-04-27",1], ["2012-04-28",1], ["2012-04-29",1], ["2012-04-30",2]]

我的脚本不工作。如何正确生成用户未登录的日期?

感谢您的时间。

4

1 回答 1

1

使用循环遍历连续日期,使用如下函数:

$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 day");

对于每个周期,应用您的查询结果。然后你会得到所有的日期。您仍将使用循环;但基础将是一天的进展,而不是您的查询结果。

伪代码:

for ($day_counter = 0; $day_counter < 365; $day_counter++) {

    $day_this = strtotime(date("Y-m-d", strtotime($day_this)) . " +1 day");
    $login_count = mysql_query("SELECT COUNT(DATE(login)) AS date_login 
                FROM statistics 
                WHERE date_login=".$day_this);

    array_push($days, array($day_this=>$login_count);

}

不确定这部分的外观:

COUNT(DATE(login))  

...但想法是您将日期时间值转换为简单日期,然后运行计数。

更正:

COUNT(DATE(login)) 错误。您必须将 COUNT() 函数应用于不同的记录字段(通常是主键),并在 WHERE 子句中使用将日期/时间折叠为简单日期的函数。我经常从我上面写的开始,然后弄清楚。:-)

于 2012-04-30T00:00:06.357 回答