我有一个名为ID的列,我希望它指示以格式创建记录的年份。
场景
I created new data on this day( 1-11-2019) and with the format.
2019-01-110000-1
当达到9时,它将变为0000-00010
示例:
20190101-0000-0001
...
20190101-0000-0010
查看
<?= $purchase_order->date . sprintf("%06s",-$counter) ?>
问题:如何在0000-1中添加前导零(应该是这样的0000-0001,当它达到9时它将变成0000-0010
答案 0 :(得分:0)
数学。
function weirdNumberFormatter(int $input) {
$high = intdiv($input, 10000);
$low = $input % 10000;
return sprintf('%04d-%04d', $high, $low);
}
var_dump(
weirdNumberFormatter(1),
weirdNumberFormatter(1234),
weirdNumberFormatter(12345),
weirdNumberFormatter(1234567),
weirdNumberFormatter(12345678)
);
输出:
string(9) "0000-0001"
string(9) "0000-1234"
string(9) "0001-2345"
string(9) "0123-4567"
string(9) "1234-5678"