PHP snippets

Friendly file size string

 1 public static function bytesToSize($bytes)
 2 {
 3         if ($bytes < 1024) {
 4             return ‘< 1k‘;
 5         } else if ($bytes < 1048576) { // 1024 * 1024
 6             return round($bytes/1024, 2) .‘K‘;
 7         } else if ($bytes < 1073741824) { // 1024 * 1024 * 1024
 8             return round($bytes/1048576, 2) .‘M‘;
 9         } else {
10             return round($bytes/1073741824, 2) .‘G‘;
11         }
12 }

Duration of the song

 1 public static function secToStr($sec)
 2 {
 3         $hours = floor($sec / 3600);
 4         $minutes = floor(($sec / 60) % 60);
 5         $seconds = $sec % 60;
 6         
 7         if ($hours > 0) {
 8             return sprintf("%02d:%02d:%02d", $hours, $minutes, $seconds);
 9         } else if ($minutes > 0) {
10             return sprintf("%d:%02d", $minutes, $seconds);
11         } else {
12             return sprintf("%d:%02d", $minutes, $seconds);
13         }
14 }

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。