Convert an array bytes to MB PHP

I have this php file which i use to get downloaded and uploaded data usage from my splynx radius server over API which outputs it as bytes making a very long string which i want to shorten into Mbps and from the arrays that i get from splynx server i would like help to convert the array " in_bytes " and " out_bytes " to MB . This is my whole code below how can i modify this code?

<?php

$api_url = 'https://xxx/'; // Splynx URL

$admin_login = "xxx"; // administrator login
$admin_password = "xxx"; // administrator password

$api = new SplynxAPI($api_url);
$api->setVersion(SplynxApi::API_VERSION_2);

$isAuthorized = $api->login([
    'auth_type' => SplynxApi::AUTH_TYPE_ADMIN,
    'login' => $admin_login,
    'password' => $admin_password,
]);

if (!$isAuthorized) {
    exit("Authorization failed!\n");
}

$customerApiUrl_online = "admin/customers/customers-online";

$customers_params = [
    'main_attributes' => [
        'status' => ['IN', ['active', 'blocked']]
    ]];

$result_online = $api->api_call_get($customerApiUrl_online);
$customers_online = $api->response;
?>

<table class="table table-transparent">
    <thead>
        <tr>
            <th>DOWNOAD</th>
            <th>UPLOAD</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($customers_online as $item) :?>
            <tr>
                <td><?php echo $item['in_bytes']; ?></td>
                <td><?php echo $item['out_bytes']; ?></td>
            </tr>
        <?php endforeach;?>
    </tbody>
</table>
`

There is a lot to find on the internet. For example:

function formatBytes($bytes, $precision = 2) { 
    $units = array('B', 'KB', 'MB', 'GB', 'TB'); 

    $bytes = max($bytes, 0); 
    $pow = floor(($bytes ? log($bytes) : 0) / log(1024)); 
    $pow = min($pow, count($units) - 1); 

    $bytes /= pow(1024, $pow);

    return round($bytes, $precision) . ' ' . $units[$pow]; 
} 

// example usage for the function
echo formatBytes((1073741824), 2); // 1 GB
1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service