diff --git a/src/Node.php b/src/Node.php index 76fbf8d..5fb27e3 100644 --- a/src/Node.php +++ b/src/Node.php @@ -155,8 +155,8 @@ function __construct() { $blockInfo = $bitcoind->getblock($blockchainInfo["bestblockhash"]); $this->bHeightAgo = round((time()-checkInt($blockInfo["time"]))/60, 1); $this->bcSize = bytesToGb($blockchainInfo["size_on_disk"], 1); - $this->diff = checkInt($blockchainInfo["difficulty"]); - $this->hashRate = round(checkInt($miningInfo["networkhashps"])/1000000000000000000,3); + $this->diff = formatDiff($blockchainInfo["difficulty"], 2); + $this->hashRate = formatHash($miningInfo["networkhashps"], 3); $this->mNetTime = getDateTime($blockchainInfo["mediantime"]); // Blockchain -> Soft forks if(isset($blockchainInfo["softforks"])) { @@ -166,3 +166,5 @@ function __construct() { } } } + + diff --git a/src/Utility.php b/src/Utility.php index 1a107b1..31e613a 100644 --- a/src/Utility.php +++ b/src/Utility.php @@ -216,6 +216,36 @@ function bytesToGb($size, int $round = 1){ return $size; } +function formatDiff($diff, int $round = 1){ + // Define the units ('', Kb, Mb, Gb, Tb, Pb, Eb) + $units = array('', 'Kb', 'Mb', 'Gb', 'Tb', 'Pb', 'Eb'); + $i = 0; + + // Loop until the hashrate is less than 1000, incrementing the unit + while ($diff >= 1000 && $i < count($units) - 1) { + $diff /= 1000; + $i++; + } + + // Format the number with the specified precision and append the unit + return number_format($diff, $round) . ' ' . $units[$i]; +} + +function formatHash($hashes, int $round = 1){ + // Define the units (H/s, KH/s, MH/s, GH/s, TH/s, PH/s, EH/s) + $units = array('H/s', 'KH/s', 'MH/s', 'GH/s', 'TH/s', 'PH/s', 'EH/s', 'ZH/s'); + $i = 0; + + // Loop until the hashrate is less than 1000, incrementing the unit + while ($hashes >= 1000 && $i < count($units) - 1) { + $hashes /= 1000; + $i++; + } + + // Format the number with the specified precision and append the unit + return number_format($hashes, $round) . ' ' . $units[$i]; +} + function getDateTime($timestamp){ $date = date("Y-m-d H:i:s",(int) $timestamp); return $date; @@ -721,3 +751,6 @@ function compare($a, $b) } ?> + + + diff --git a/views/main.phtml b/views/main.phtml index 74c4a86..c485f64 100644 --- a/views/main.phtml +++ b/views/main.phtml @@ -113,7 +113,7 @@ Chain chain ?> Size bcSize ?> GB Difficulty diff ?> - Hashrate hashRate ?> EH/s + Hashrate hashRate ?> Mediantime mNetTime ?> @@ -352,3 +352,4 @@ +