【问题标题】:Need PHP Function that converts sizes from byte to yottabyte and reverse需要将大小从字节转换为 yottabyte 并反转的 PHP 函数
【发布时间】:2014-04-15 14:10:16
【问题描述】:

类似的东西

function convert_size($from,$to,$unit){

some code...

return $calculated_value

}

echo convert_size("YB","KB",50);
  • 字节 (B)
  • 千字节 (KB)
  • 兆字节 (MB)
  • 千兆字节 (GB)
  • 太字节 (TB)
  • 拍字节 (PB)
  • 艾字节 (EB)
  • Zettabyte (ZB)
  • Yottabyte (YB)

【问题讨论】:

  • 好的。你试过什么?
  • 这里有一些东西可以用来比较你的结果:50 Yottabytes = 50,000,000,000,000,000,000,000 KB。

标签: php size byte calculator


【解决方案1】:

要获得更简洁的方法,请使用此功能:

function convert_size($from, $to, $unit)
{
    $units = array(
        'B' => 1,
        'KB' => 1024,
        'MB' => 1024^2,
        'GB' => 1024^3,
        // fill with more units you need
    );

    return ($unit*$units[$from])/$units[$to];
}

例如将 3Gb 转换为 MB,最终公式应为 (3×(1024^3))÷(1024^2),结果为 3072MB。

【讨论】:

    【解决方案2】:

    怎么样:

    function convert_size($from,$to,$unit){
      $units = array('B', 'KB', 'MB', 'GB', 'TB','PB','EB','ZB','YB');
      if(($fromPosition = array_search($from,$units)) === false){
         return false;
      }
      if(($toPosition = array_search($to,$units)) === false){
        return false;
      }
      $diffUnits = $fromPosition-$toPosition;
      return $unit * (pow(1024, $diffUnits));
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-24
      • 2014-08-08
      相关资源
      最近更新 更多