254

PHP - Is there a quick, on-the-fly method to test for a single character string, then prepend a leading zero?

Example:

$year = 11;
$month = 4;

$stamp = $year.add_single_zero_if_needed($month);  // Imaginary function

echo $stamp; // 1104
1
  • 3
    After testing, turns out sprintf() was a bit better: it has a common language format and doesn't use a class constant, among other things.
    – Ben
    Jan 15, 2013 at 7:09

3 Answers 3

610

You can use sprintf: http://php.net/manual/en/function.sprintf.php

<?php
$num = 4;
$num_padded = sprintf("%02d", $num);
echo $num_padded; // returns 04
?>

It will only add the zero if it's less than the required number of characters.

Edit: As pointed out by @FelipeAls:

When working with numbers, you should use %d (rather than %s), especially when there is the potential for negative numbers. If you're only using positive numbers, either option works fine.

For example:

sprintf("%04s", 10); returns 0010
sprintf("%04s", -10); returns 0-10

Where as:

sprintf("%04d", 10); returns 0010
sprintf("%04d", -10); returns -010

6
  • 11
    Beware of negative numbers (obviously not a problem with dates here! :) ). %03s can output 0-5 where %03d would output -05
    – FelipeAls
    Jul 11, 2014 at 9:09
  • this doesn't work with negative numbers
    – Rafik Bari
    Feb 17, 2015 at 19:39
  • @RafikBari I've updated my answer to include the information @FelipeAls mentioned. Negative numbers are handled fine, provided you use %02d.
    – Kirk Beard
    Feb 17, 2015 at 19:53
  • 2
    Your edit is wrong, sprintf("%04s", 10); returns 0010, not 1000. Oct 27, 2015 at 18:40
  • 1
    Also sprintf("%04d", -10); returns -010. Tested on PHP 5.5.18 Oct 27, 2015 at 18:42
230

You can use str_pad for adding 0's

str_pad($month, 2, '0', STR_PAD_LEFT); 

string str_pad ( string $input , int $pad_length [, string $pad_string = " " [, int $pad_type = STR_PAD_RIGHT ]] )

3
  • 4
    Pure gold right here!
    – Jordan
    Jul 27, 2013 at 22:28
  • 4
    @zzlalani - actually if you look carefully at the example in Kirk Beard's answer, it is saved to a variable :)
    – Ben
    Sep 14, 2013 at 7:42
  • this doesn't work with negative nums
    – Rafik Bari
    Feb 17, 2015 at 19:35
18

The universal tool for string formatting, sprintf:

$stamp = sprintf('%s%02s', $year, $month);

http://php.net/manual/en/function.sprintf.php

3
  • 2
    str_pad is more readable. I like keeping things simple. sprintf is great function, i use it a lot for transaltions, when there must be a variable in the middle of the string, but for such thing, i will go with str_pad(); while i'm working only with positive integers. they don't require any fancy stuff. Apr 4, 2015 at 21:49
  • Use sprintf: sprintf('%08d', 1234567); Alternatively you can also use str_pad: str_pad($value, 8, '0', STR_PAD_LEFT);
    – Kamlesh
    Sep 4, 2019 at 16:32
  • what about signed digit ? str_replace("00", "0", sprintf('%03d', $num))
    – Sahib Khan
    Jun 5, 2020 at 6:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.