Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format numbers with 00 prefixes in php?

I'm trying to generate invoice numbers. They should always be 4 numbers long, with leading zeros, for example :

  • 1 -> Invoice 0001
  • 10 -> Invoice 0010
  • 150 -> Invoice 0150

etc.

like image 374
mike23 Avatar asked Jun 09 '11 17:06

mike23


People also ask

How can I print 01 instead of 1 in PHP?

You can use sprintf("%02d", $number) to format strings.

How can I get 2 digit number in PHP?

Use sprintf() Function to Show a Number to Two Decimal Places in PHP. Copy sprintf($format, $parameter1, $parameter2, ... , $parameterN); The parameter $format is the format that specifies how the variables will be in the string.


1 Answers

Use str_pad().

$invID = str_pad($invID, 4, '0', STR_PAD_LEFT);

like image 95
Wiseguy Avatar answered Sep 27 '22 17:09

Wiseguy