點下面的按鈕可以加入我們!
str_pad($num, 11, "D0", STR_PAD_LEFT);
<?php final class GID { private $cfile = ""; private $cym = ""; public function __construct () { $this->cfile = date("ym"); // 檔案,看你要放哪個位置自己喬一下 $this->cym = "D".date("ym")."-"; // 你的前置格式 } public function generateID () { if (file_exists($this->cfile)) { $fp = fopen($this->cfile, "r"); $id = intval(fread($fp, filesize($this->cfile))); fclose($fp); } $id ++; $fp = fopen($this->cfile, "w"); flock($fp, LOCK_EX); fputs($fp, $id); flock($fp, LOCK_UN); fclose($fp); return $this->cym . sprintf("%04d", $id); } } $GID = new GID(); echo $GID->generateID(); ?>
評論
直至目前為止,我處理流水號的做法還是,當字串處
$aa="1".substr($id,-4);
$aa++;
$num="U".strval(substr($aa,-4));
先取出流水號前面隨變加個數字
然後 + 1
然後強制轉換成字串輸出
這樣前面的 0 就不會不見
不過看起來還是 kiang 大貼的函數實用!!
你參考看看
那你要先去查出來有沒有當月再下判斷