PHP Count round thousand to a K style count like facebook Share . . . Twitter Button ect

后端 未结 12 1798
北恋
北恋 2020-12-13 05:15

Ok so I am trying to turn my hit counter to round thousands to a single digit too display 3 thousand hits as 3K for example, like the Facebook Share and Twitter Tweet Button

12条回答
  •  时光说笑
    2020-12-13 05:54

    function print_number_count($number) {
        $units = array( '', 'K', 'M', 'B');
        $power = $number > 0 ? floor(log($number, 1000)) : 0;
        if($power > 0)
            return @number_format($number / pow(1000, $power), 2, ',', ' ').' '.$units[$power];
        else
            return @number_format($number / pow(1000, $power), 0, '', '');
    }
    

提交回复
热议问题