-1

Possible Duplicate:
Zero-pad digits in string

I'm using this count script to show the number of new messages a user has. the script shows a number value of however many new messages the user has by using count.

At the moment if the number is below 10 the number will be show as 1, 2, 3 etc but i want it to be show as 01, 02, 03, 04 etc in double digits.

Is this possible. Here's the code i have so far?

<?php
$check_new_messages_set = check_new_messages();
while ($new = mysql_fetch_array($check_new_messages_set)) {

echo "<div class=\"msg-notify\">". $new['COUNT(id)'] ."</div>"; 
?>
Community
  • 1
  • 1
Tommy Lincoln
  • 37
  • 1
  • 8

2 Answers2

7

Use sprintf() for simple padding:

$padded = sprintf('%02d', $number); // 2 -> 02
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

Another option is str_pad, either way you're going to end up with a string but if its just for display that's not a problem

ste
  • 286
  • 1
  • 2
  • 9