leachus2002 Posted February 2, 2011 Share Posted February 2, 2011 Hi All, I am trying to "echo" a result from a SQL query, that returns a size in KB, but I want this to read in MB or GB - can PHP change this for me, or is it something I need to do in SQL? Cheers Matt Quote Link to comment https://forums.phpfreaks.com/topic/226462-converting-kb-to-mbgb/ Share on other sites More sharing options...
nankoweap Posted February 2, 2011 Share Posted February 2, 2011 assuming the column is a number, you could do it in the sql. select col as kb, col/1024 as mb, col/(1024 * 1024) as gb from ... probably some formatting sql functions too, but you could do it php if needed. Quote Link to comment https://forums.phpfreaks.com/topic/226462-converting-kb-to-mbgb/#findComment-1168881 Share on other sites More sharing options...
ManiacDan Posted February 2, 2011 Share Posted February 2, 2011 Right...there's 1024 kb in a mb. Division is built into every language. Quote Link to comment https://forums.phpfreaks.com/topic/226462-converting-kb-to-mbgb/#findComment-1168887 Share on other sites More sharing options...
jamesxg1 Posted February 2, 2011 Share Posted February 2, 2011 function file_size($size) { $filesizename = array(" Bytes", " KB", " MB", " GB", " TB", " PB", " EB", " ZB", " YB"); return $size ? round($size/pow(1024, ($i = floor(log($size, 1024)))), 2) . $filesizename[$i] : '0 Bytes'; } Or try this; http://www.linein.org/blog/2008/06/03/quick-php-function-to-get-file-sizes-in-kb-mb-gb/ James. Quote Link to comment https://forums.phpfreaks.com/topic/226462-converting-kb-to-mbgb/#findComment-1168893 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.