简体   繁体   中英

How to Select all Timestamps grouped by Date(Timestapms) PHP/MYSQL

so I want to select all timestamps in each days of the last 7 days and put them in a json array, the code that I'm using is :

$sql = "SELECT Time from gps_rt WHERE UID='$uid' group by Date(Time) LIMIT 7";

  $result = $link->query($sql);

   while ($pdays = mysqli_fetch_array($result)) {

   $days['Durees'][$i] = $pdays['Time'];

   $i=$i+1;   
  }

 echo json_encode($days);

the problem is that this code shows me only the first Timestamps of each day, and for me I need to get All the Timestapms of each day :

  Durees    
       0    "2018-06-14 12:44:03"
       1    "2018-06-15 11:06:07"
       2    "2018-06-16 20:40:08"
       3    "2018-06-17 18:33:44"
       4    "2018-06-18 08:35:44"
       5    "2018-06-19 08:09:46"
       6    "2018-06-20 10:11:46"

What I want is :

          Durees    
             0  "2018-06-14 12:44:03"
                "2018-06-14 12:44:13"
                "2018-06-14 12:48:03"
                           .
                           .
                           .
             1  "2018-06-15 11:06:07"
                "2018-06-15 11:08:17"
                           .
                           .
                           .
             2  "2018-06-16 20:40:08"
                           etc

You must setting first the time interval. After you can query data from database, but in array can you set only one value for a key. Your wish maybe not possible.

Try this solution, maybe help for you:

<?php

/* Detect first and last timestamp of the day (7 days before) -- */
$current_day_start = strtotime(date("d.m.Y 00:00:00"));
$current_day_end =  strtotime(date("d.m.Y 23:59:59"));
$seven_days_before_start = $current_day_start - ( 7 * 86400 );
$seven_days_before_end =  $current_day_end - ( 7 * 86400 );

/* Define the Array -------------------------------------------- */
$days = array();

/* Create loop for last 7 days --------------------------------- */
for ($i = 0; $i < 7; $i++) {

   $sql = "SELECT Time from gps_rt WHERE UID='$uid' AND Time > '$seven_days_before_start' AND Time < '$seven_days_before_end'";
   $result = mysqli_query($link, $sql);

   while ($pdays = mysqli_fetch_array($result)) {

      $days[$i]['Durees'][] = $pdays['Time'];

   }

   $seven_days_before_start += 86400;
   $seven_days_before_start += 86400;

}

/* Generate JSON content --------------------------------------- */
json_decode($days);


/* I hope the result is like this: -----------------------------

$days [ "NUMBER OF DAYS" ] [ "Durees" ] [ "NUMBERS" ]

$days = Array 

0 -> Durees
    0 -> "2018-06-14 12:44:03"
    1 -> "2018-06-14 12:44:13"
    3 -> "2018-06-14 12:48:03"
                       .
                       .
                       .
1 -> Durees
    0 -> "2018-06-15 11:06:07"
    1 -> "2018-06-15 11:08:17"
                       .
                       .
                       .
2 -> Durees
    2 -> "2018-06-16 20:40:08"

etc

*/

?>

More info: how to store multiple values for one key in php array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM