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

后端 未结 12 1856
北恋
北恋 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:44

    Use floor instead of round if you want 3500 to round down to 3 K.

    Otherwise, your code works, albeit problematically. Try this:

    if ($postresultscount > 1000) {
      $result = floor($postresultscount / 1000) . 'K';
    } else {
      $result = $postresultscount;
    }
    
    echo 'document.write("' . $result . '")";
    

    It also appears you're writing JavaScript using PHP—take care.

提交回复
热议问题