将缺少的日子填入阵列

时间:2012-04-29 23:57:52

标签: php arrays time

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日。我必须在两个日期生成那些缺失的日子。

我目前的算法似乎不起作用。我无法弄清楚出了什么问题。

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

我将所有值存储到数组$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]]

我的脚本无效。如何正确生成用户未登录的日期?

感谢您的时间。

1 个答案:

答案 0 :(得分: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(登录))错误。必须将COUNT()函数应用于不同的记录字段(通常是主键),并使用将日期/时间折叠为WHERE子句中的简单日期的函数。我经常从上面写的内容开始,然后想出来。 : - )