PHP版本的自动生成有规则的订单号(或编号)
生活随笔
收集整理的這篇文章主要介紹了
PHP版本的自动生成有规则的订单号(或编号)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
<?php
/**
*?PHP版本的自動(dòng)生成有規(guī)則的訂單號(hào)(或編號(hào))
*?生成的格式是:?20130103000001?前面幾位為當(dāng)前的日期,后面6位為系統(tǒng)自增長類型的編號(hào)
*?原理:?
*?1.獲取當(dāng)前日期格式化值;
*?2.讀取文件,上次編號(hào)的值+1最為當(dāng)前此次編號(hào)的值(記錄以文件的形式存儲(chǔ))
*?(下月會(huì)接著這個(gè)編號(hào))
*/class?FileEveryDaySerialNumber?{private?$filename;?//文件名?private?$separate;?//系統(tǒng)分隔符private?$width;?//自動(dòng)增長部分的個(gè)數(shù)public?function?__construct($width,?$filename,?$separate)?{$this->width?=?$width;$this->filename?=?$filename;$this->separate?=?$separate;}public?function?getOrUpdateNumber($current,?$start)?{$record?=?IOUtil::read_content($this->filename);$arr?=?explode($this->separate,?$record);if($current?==?$arr[0]){?//如果是同一天,則繼續(xù)增長$arr[1]++;IOUtil::write_content("$arr[0],$arr[1]",?$this->filename);?//將新值存入文件中return?"$arr[0]".str_pad($arr[1],$this->width,0,STR_PAD_LEFT);}else{?//如果兩個(gè)日期不一樣則重新從起始值開始$arr[0]?=?$current;$arr[1]?=?$start;IOUtil::write_content("$arr[0],$arr[1]",?$this->filename);?//將新值存入文件中return?"$arr[0]".str_pad($arr[1],$this->width,0,STR_PAD_LEFT);}}?
}
class?IOUtil{public?static?function?read_content($filename){$handle?=?fopen($filename,"r");$content?=?fread($handle,filesize($filename));return?$content;}public?static?function?write_content($content,?$filename){$handle?=?fopen($filename,"w");fseek($handle,0);fwrite($handle,?$content);return?$content;}
}//測(cè)試代碼
//參數(shù)含義分別是日期后自增長數(shù)的位數(shù),?存儲(chǔ)的文件名稱,?日期與自增長數(shù)的分割數(shù)
$obj?=?new?FileEveryDaySerialNumber(6,"EveryDaySerialNumber.dat",",");?
$current_date?=?date("Ymd");
echo?$obj->getOrUpdateNumber($current_date,1);?>
轉(zhuǎn)載于:https://blog.51cto.com/happyliu/1585362
總結(jié)
以上是生活随笔為你收集整理的PHP版本的自动生成有规则的订单号(或编号)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Add Binary
- 下一篇: GHOST分区丢失只剩C盘