0

I am creating a 2D array, in which the first Value shows the time and the other one shows the Random no. with that value. I am creating multiple arrays at the same load using for loop. but the problem is that the time stamp in all the array values prints the same.

so for every array values in the for loop, the time is same.

Here is my Code:

<?php
header("Content-type: text/json");

$arr = [] ;
for($i=0 ;$i<5;$i++)
{
   $time = [] ;
   $time[] = date('Y-m-d H:i:s') ;
    $random_no = [];
    $random_no[] = rand(0,10) ;
   $arr[] = array_merge($time,$random_no);
}
echo json_encode($arr); 
 ?>

The output I am getting is somewhat like :

[["2019-09-05 07:42:24",8],["2019-09-05 07:42:24",1],["2019-09-05 07:42:24",1],["2019-09-05 07:42:24",3],["2019-09-05 07:42:24",0]]

Note that Time displays the same in every array Value. I just want the time values to be different in an incrementing order

Chempooro
  • 415
  • 1
  • 7
  • 24
  • 1
    possibly the for loop is faster then 1 sec.. can you try add milliseconds to your date format? see this answer for how https://stackoverflow.com/a/17909891/2008111 or may simpler change the `5` in the for loop to `5000000` – caramba Sep 05 '19 at 05:48
  • Simple way to pick date time before loop and add seconds using for loop variable $i. – Dark Knight Sep 05 '19 at 05:57

3 Answers3

2

You can try using microtime because your for loop gets completed before your time changes.

<?php
    header("Content-type: text/json");

    $arr = [] ;
    for($i=0 ;$i<5;$i++)
    {
       $time = [] ;
       $now = DateTime::createFromFormat('U.u', microtime(true));

       $time[] = $now->format("Y-m-d H:i:s.u");
       $random_no = [];
       $random_no[] = rand(0,10) ;
       $arr[] = array_merge($time,$random_no);
    }
    echo json_encode($arr); 
?>
Rahul
  • 18,271
  • 7
  • 41
  • 60
mrid
  • 5,782
  • 5
  • 28
  • 71
1

Your for(...) execution is getting completed before the next second. That's the way, you are getting the same date.

By alternatively you can try using sleep()

header("Content-type: text/json");
$arr = [];

for($i = 0; $i < 5; $i++) {
    sleep(1);
    $arr[] = [date('Y-m-d H:i:s'), rand(0, 10)];
}
echo json_encode($arr);

Note: sleep() is not good attempt because it delays the execution of codes.

Another approach is to reate a dateTime object and add by 1 second or some other seconds/minutes/hour....

header("Content-type: text/json");
$time = new DateTime();
$arr = [$time->format('Y-m-d H:i:s'), rand(0, 10)];

for($i = 0; $i < 4; $i++) {
    $time->modify('1 seconds');
    $arr[] = [$time->format('Y-m-d H:i:s'), rand(0, 10)];
}
echo json_encode($arr);
MH2K9
  • 11,951
  • 7
  • 32
  • 49
0

Datetime has been working with microseconds since PHP 7.1. The detour via microtime(true) is no longer necessary.

$arr = [] ;
for($i=0 ;$i<5;$i++)
{
   $arr[] = [
     date_create('now')->format("Y-m-d H:i:s.u"),
     rand(0,10)
   ];
}

//Test output
echo "<pre>".json_encode($arr, JSON_PRETTY_PRINT)."</pre>"; 

Example of an output

[
    [
        "2019-09-05 09:10:34.381645",
        2
    ],
    [
        "2019-09-05 09:10:34.381686",
        2
    ],
    [
        "2019-09-05 09:10:34.381694",
        5
    ],
    [
        "2019-09-05 09:10:34.381700",
        9
    ],
    [
        "2019-09-05 09:10:34.381706",
        7
    ]
]
jspit
  • 7,276
  • 1
  • 9
  • 17