[英]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-05
到2016-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.