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]){ //如果是同一天,则继续增长
$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{ //如果两个日期不一样则重新从起始值开始
$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;
}
}
//测试代码
//参数含义分别是日期后自增长数的位数, 存储的文件名称, 日期与自增长数的分割数
$obj = new FileEveryDaySerialNumber(6,"EveryDaySerialNumber.dat",",");
$current_date = date("Ymd");
echo $obj->getOrUpdateNumber($current_date,1);
?>
文章名称:PHP版本的自动生成有规则的订单号(或编号)
当前地址:
http://chengdu.cdxwcx.cn/article/pdcedd.html