0

It's been a long time since i asked a question here, because most of the answers to my questions can already be found.

But today i need help

I'm working with these set of data array:

array (
  '_metadata' => 
  array (
    'from' => '2018-11-30T16:00:00+00:00',
    'to' => '2018-12-08T11:05:40+00:00',
    'granularity' => 'DAY',
    'aggregation' => 'SUM',
  ),
  'metrics' => 
  array (
    'DOWN_PUBLIC' => 
    array (
      'unit' => 'B',
      'values' => 
      array (
        0 => 
        array (
          'value' => 23501281,
          'timestamp' => '2018-12-02T16:05:00+00:00',
        ),
        1 => 
        array (
          'value' => 221147881,
          'timestamp' => '2018-12-03T16:05:00+00:00',
        ),
        2 => 
        array (
          'value' => 104525623,
          'timestamp' => '2018-12-04T16:05:00+00:00',
        ),
        3 => 
        array (
          'value' => 163306933,
          'timestamp' => '2018-12-05T16:05:00+00:00',
        ),
        4 => 
        array (
          'value' => 159806208,
          'timestamp' => '2018-12-06T16:05:00+00:00',
        ),
        5 => 
        array (
          'value' => 122203658,
          'timestamp' => '2018-12-07T16:05:00+00:00',
        ),
      ),
    ),
    'UP_PUBLIC' => 
    array (
      'unit' => 'B',
      'values' => 
      array (
        0 => 
        array (
          'value' => 111801,
          'timestamp' => '2018-12-02T16:05:00+00:00',
        ),
        1 => 
        array (
          'value' => 5594252,
          'timestamp' => '2018-12-03T16:05:00+00:00',
        ),
        2 => 
        array (
          'value' => 11654085,
          'timestamp' => '2018-12-04T16:05:00+00:00',
        ),
        3 => 
        array (
          'value' => 10870143,
          'timestamp' => '2018-12-05T16:05:00+00:00',
        ),
        4 => 
        array (
          'value' => 9288760,
          'timestamp' => '2018-12-06T16:05:00+00:00',
        ),
        5 => 
        array (
          'value' => 9288262,
          'timestamp' => '2018-12-07T16:05:00+00:00',
        ),
      ),
    ),
  ),
)

I need to convert the timestamp to date (ie; 3 Dec) and the value(bytes) to convert to megabytes, then pass it to javascript as an array, so the output will be something like:

$dateConverted = ['3 Dec', '4 Dec', '5 Dec', '6 Dec', '7 Dec'];
$megabytesDown = [23.50, 221.15, 104.53, ...];
$megabytesUp = [0.11, 5.59, 11.65, ...];

Using Dexter's Answer below, cleaned it up a little got it working great

function custDateFormat($date){
            return date('d M', strtotime($date));
        }

        function bytes_to_mb($bytes, $decimal_places = 1 ){
            return number_format($bytes / 1048576, $decimal_places);
        }
        $bigArray = json_decode($Dataresponse);
        $dateConverted = [];
        $megabytesDown = [];
        $megabytesUp   = [];
        $upVal   = $bigArray->metrics->UP_PUBLIC->values;
        $downVal = $bigArray->metrics->DOWN_PUBLIC->values;

        foreach($bigArray->metrics->UP_PUBLIC->values as $key => $val)
        {
            $dateConverted[] = custDateFormat($val->timestamp);
            $megabytesDown[] = bytes_to_mb($downVal[$key]->value);
            $downTotal+= bytes_to_mb($downVal[$key]->value);
            $megabytesUp[]  = bytes_to_mb($upVal->value);
            $upTotal+= bytes_to_mb($upVal->value);
        }

Passing the results to Chart.js, outcome:

network traffic chart

cyben
  • 43
  • 1
  • 4
  • Convert iso8601 string to date in php: [here](https://stackoverflow.com/questions/38006169/convert-string-iso-to-date-php), Convert bytes to megabytes: [here](https://stackoverflow.com/questions/2365100/converting-bytes-to-megabytes), pass array from php to javascript: [here](https://stackoverflow.com/questions/23740548/how-to-pass-variables-and-data-from-php-to-javascript) – James Dec 08 '18 at 15:02

1 Answers1

0
function custDateFormat($date){
    return date('d M', strtotime($date));
}

function convertToReadableSize($size){
  $base = log($size) / log(1024);
  return round(pow(1024, $base - floor($base)), 1);
}

$dateConverted = [];
$megabytesDown = [];
$megabytesUp   = [];

$upVal   = $bigArray['UP_PUBLIC']['values'];
$downVal = $bigArray['DOWN_PUBLIC']['values'];

foreach($bigArray['UP_PUBLIC']['values'] as $key => $val)
{
    $dateConverted[] = custDateFormat($val['timestamp']);
    $megabytesDown[] = convertToReadableSize($downVal[$key]['value']);
    $megabytesUp[]   = convertToReadableSize($val['value']);
}
oreopot
  • 3,392
  • 2
  • 19
  • 28