public static function generateReceiptNumber(int $id)
{
$receipt_number = sprintf('%06d', $id % 100000000);
return $receipt_number;
}
I am having the above code to help me to transform a passed in $id to a minimum 6 digit and maximum 8 digit number. eg: 000001 – 99999999
But this code has a flaws that when the $id equal to 100000000, it will return me 000000,
how can i enhance the code above to give me 000001 instead?
So and so forth, the $id is the database incremental id
4
Answers
please check this answer if it will help you.
This method does not have maximum cap, you can use simply add a condition to do so if really needs
How about this:
Just use the
max
andmin
functions to make it an elegant one liner.Live Demo