Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fbc's avatar
Level 2

How do I apply date('H:i:s', $this_array)??

I have this array:

$realtime_labels_array = $latestfeed72->pluck('time')->toArray();

I'd like to pluck 'time' in this format

date('H:i:s', $this_array)
0 likes
10 replies
fbc's avatar
Level 2

@D9705996 - Thanks!!! I tried it but now my Y axis on my chart says "object object"

        $latestfeed72 = Feed72::orderBy('time', 'desc')->take(100)->get();
        $realtime_labels_array = $latestfeed72->pluck('time')->toArray();
        $realtime_data_array = $latestfeed72->pluck('data')->toArray();

        $realtime_consumption_chart = new ChartJS;
        $realtime_consumption_chart->labels($realtime_labels_array);
        $realtime_consumption_chart->dataset('My dataset', 'line', $realtime_data_array);

This is what the complete use is like..

the time column is in UNIX/EPOCH time. This is why I'm trying to convert it.

D9705996's avatar

You need to ensure you are passing the data from your backend to you frontend in the format chart.js expects. Have a tinker with console.log in you front end to see what's going wrong

Cronix's avatar

What is time? Is it a carbon object, a string? Is it only time, or a datetime?

Does this work for you?

$realtime_labels_array = $latestfeed72->pluck('time')->toArray();
$realtime_labels_array = array_map(function($t) { return date("H:i:s", strtotime($t)); }, realtime_labels_array);
fbc's avatar
Level 2

@CRONIX - I tried this:

        $realtime_labels_array = array_map(function($t) { return date("H:i:s", strtotime($t)); }, $realtime_labels_array);

but it turned all my labels to zeros.

Cronix's avatar

It would help to show the output of

$realtime_labels_array = $latestfeed72->pluck('time')->toArray();
dd($realtime_labels_array);

I was guessing because you didn't show what data you are actually working with.

$realtime_labels_array = array_map(function($item) {
    return [
        'time'=> date('H:i:s', strtotime($item['time'])),
        'data' => $item['data']
    ];
}, $times);

Maybe that will work, but I don't know if you're working with an array of arrays or an array of objects. If objects, you'd have to use object notation instead of array notation like I'm showing.

fbc's avatar
Level 2

@CRONIX - This is what I've got:

array:100 [▼
  0 => 1552079470
  1 => 1552079460
  2 => 1552079450
  3 => 1552079440
  4 => 1552079430
  5 => 1552079420
  6 => 1552079410
  7 => 1552079400
  8 => 1552079390
  9 => 1552079380
  10 => 1552079370
  11 => 1552079360
  12 => 1552079350
  13 => 1552079340
  14 => 1552079330
  15 => 1552079320
  16 => 1552079310
  17 => 1552079300
  18 => 1552079290
  19 => 1552079280
  20 => 1552079270
  21 => 1552079260
  22 => 1552079250
  23 => 1552079240
  24 => 1552079230
Cronix's avatar
Cronix
Best Answer
Level 67

This will convert the array you gave (using first 4 timestamps from your post) into the same array using H:i:s. I don't know if it's what you need for charts to work, but it should do what your original question asks according to my test shown below.

>>> $dates = [1552079470, 1552079460, 1552079450, 1552079440];
=> [
     1552079470,
     1552079460,
     1552079450,
     1552079440,
   ]

>>> $formattedDates = array_map(function($time) { return date("H:i:s", $time); }, $dates);
=> [
     "21:11:10",
     "21:11:00",
     "21:10:50",
     "21:10:40",
   ]
fbc's avatar
Level 2

@CRONIX - Is there any way to change:

        $latestfeed72 = Feed72::orderBy('time', 'desc')->take(100)->get();

to only query the records in 10 minute intervals? Right now the database contains values at 10 sec intervals.

Maybe there is a skip() that will skip over and read only every 60th record?

Disregard: Just discovered:

$collection->nth(4);

Please or to participate in this conversation.