首页 > 代码库 > PHP snippets

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 }

 

PHP snippets