简体   繁体   English

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

[英]How to store each individual day inside time period into array

Sorry if title was a bit obscure, I'll give my best to explain my problem. 抱歉,如果标题有点模糊,我会尽力解释我的问题。 I run query like this: 我这样运行查询:

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

On table like this: 在这样的桌子上:

在此处输入图片说明

So, what I need to achieve is to have all time_send values within one day into one array, so if date is 2016-04-12 to have array like this: 因此,我需要实现的是将一天之内的所有time_send值都放入一个数组中,因此,如果date是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)

And if time interval for witch I need to store those dates is let's say from 2016-04-05 to 2016-04-12 array would look like this: 如果我需要存储这些日期的时间间隔,比如说从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)

And so on for each different day inside given time interval. 依此类推,在给定的时间间隔内每个不同的日期。 So I tried doing it like this: 所以我尝试这样做:

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

But this now prints only the last date and it's time: 但这现在只显示最后的日期和时间:

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

And my database stores more results for 2016-04-12 day. 而且我的数据库存储了2016-04-12日更多的结果。 在此处输入图片说明

Please help me with this, I'm stuck. 请帮助我,我被困住了。

Your SQL query could look something like this: 您的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

To build such an array you need, you can use this part: 要构建您需要的数组,可以使用以下部分:

$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']);
}

Please note that $datalist is the array of data from the database. 请注意, $datalist是数据库中数据的数组。

You will then have an array like this: 然后,您将得到一个像这样的数组:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取每天(指定时间段内)温度最高的注册ID? - How can I take the ids of the registrations with the highest temperature of each day (on a specified period of time)? 如何为单个用户隐藏特定时间段内的记录? - How to hide records for a specific time period for individual users? 时间段占工作日的百分比 - Time period as percentage of the business day Mysql PHP - 如何计算每天记录的个人加权分数 - Mysql PHP - How to calculate individual weighted score for each day record 如何将时间存储为天跨度..as整数? - How to store time as day span..as integer? 如何检查网站是否已启动并长时间存储结果 - How to check if website is up and store results for long time period 多维数组将每个列表数组存储在另一个数组中 - multidimensional array store each list array inside another array 如何在html表(php)中从Mysql表显示特定时间段(天/周/月)的数据 - How to display data for a certain period of time (day / week / month) from Mysql table in html table (php) 如何在在线电台项目的数据库中存储日期和时间? - How to store day and time in database for online radio station project? 如何在PHP中查询数组并为每个元素返回单独的行/项目 - How to query an array in PHP and return individual rows/items for each element
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM