如何将每个时间段内的每一天存储到数组中

时间:2016-04-12 06:46:21

标签: php arrays

对不起,如果标题有点模糊,我会尽力解释我的问题。我运行这样的查询:

$queryForDateClicks="SELECT `time_send` FROM `notifications` WHERE `app_id`='$appId' AND `time_send`>'2016-04-11 23:59:59'";

在这样的桌子上:

enter image description here

所以,我需要实现的是将一天内的所有time_send值都放入一个数组中,因此如果日期为2016-04-12则具有这样的数组:

Array ( [2016-04-12] => 2016-04-12 07:45:37, 2016-04-12 08:07:03, 2016-04-12 08:13:48)

如果我需要存储这些日期的时间间隔,那么从2016-04-052016-04-12数组可以看起来像这样:

Array ( [2016-04-05] => 2016-04-05 06:45:37, 2016-04-05 11:17:30, [2016-04-06] => 2016-04-06 02:35:37, 2016-04-06 23:17:30, [2016-04-07] => 2016-04-07 06:45:37, 2016-04-07 21:17:30)

对于给定时间间隔内的每个不同日期等等。所以我试着这样做:

$queryForDateClicks="SELECT `time_send` FROM `notifications` WHERE `app_id`='$appId' AND `time_send`>'2016-04-11 23:59:59'";
    $chartResultDate=$mysqli->query($queryForDateClicks);
    $dani=array();
    while($brojDate=$chartResultDate->fetch_assoc()){
        $minutes = substr($brojDate['time_send'], -8);
        $days = substr($brojDate['time_send'], 0, 10);
        if($minutes< '23:59:59'){
        $dani[$days]=$brojDate['time_send'];
    }else{
        //should increment day by one and start checking again if minutes for that day are less than '23:59:59' and so on.
        }
    }
    print_r($dani);

但现在只打印最后一个日期和时间:

Array ( [2016-04-12] => 2016-04-12 08:40:02 )

我的数据库为2016-04-12天存储了更多结果。 enter image description here

请帮助我,我被困住了。

1 个答案:

答案 0 :(得分:1)

您的SQL查询可能如下所示:

SELECT DATE(time_send) as time_send_date, time_send
FROM notifications
WHERE time_send > '2016-04-11 23:59:59'
ORDER BY time_send

要构建您需要的阵列,您可以使用此部分:

$notificationDataArray = [];
foreach($datalist as $row){
    if(!array_key_exists($row['time_send_date'], $notificationDataArray)){
        $notificationDataArray[$row['time_send_date']] = [];
    }
    array_push($notificationDataArray[$row['time_send_date']], $row['time_send']);
}

请注意$datalist是数据库中的数据数组。

然后你会得到一个这样的数组:

array (size=2)
  '2016-04-11' => 
    array (size=3)
      0 => string '2016-04-11 13:52:23' (length=19)
      1 => string '2016-04-11 14:45:57' (length=19)
      2 => string '2016-04-11 15:41:21' (length=19)
  '2016-04-12' => 
    array (size=2)
      0 => string '2016-04-12 11:21:29' (length=19)
      1 => string '2016-04-12 18:45:30' (length=19)