Method to encapsulate numbers into 6 to 8 character limit
P粉315680565
2023-08-10 10:36:59
<p><br /></p>
<pre class="brush:php;toolbar:false;">public static function generateReceiptNumber(int $id)
{
$receipt_number = sprintf(' d', $id % 100000000);
return $receipt_number;
}</pre>
<p>I am using the above code to convert the incoming $id to a minimum 6 digit, maximum 8 digit number. For example: 000001 - 99999999</p>
<p>But there is a flaw in this code. When $id equals 100000000, it will return 000000. How should I improve the above code to return 000001? </p>
<p>By analogy, $id is the auto-incremented ID of the database. </p>
<p>The reason I want to achieve this is because I have a display text box with a text limit of only 8 digits and I can only count the numbers back up from 000001 and keep repeating. </p>
Please see if this answer helps
How about this: