这里的单号值得是单子号,例如订单号、进货单号和发货单号等,大约在两年前我写过一个ASP的通用函数,而今天我又写了一个PHP的通用函数,一并放在这里了。
函数都是通过一个已知的单子号来生成一个新的单子号,可以根据情况定制后面递增数的位数。
【经过测试,我发现原来的函数有点bug,所以做了一下修改,并把这个函数精简了一下,原来的函数太繁琐了】。
PHP函数:
function order_num($order_num,$n=4){
$length = strlen($order_num);
$time = time();
if (empty($order_num)){
return substr($order_num,0,$lenth-6-$n).date("ymd",$time).str_repeat("0",$n-1)."1";
} else {
$year = substr($order_num,$lenth - $n - 6,2);
$month = substr($order_num,$lenth - $n - 4,2);
$day = substr($order_num,$lenth - $n - 2,2);
if ($year != date("y",$time) || $month != date("m",$time) || $day != date("d",$time)){
return substr($order_num,0,$lenth-6-$n).date("ymd",$time).str_repeat("0",$n-1)."1";
} else {
$num = substr($order_num,-$n,$n);
$num = $num+1;
$num = str_repeat("0",$n-strlen($num)).$num;
return substr($order_num,0,$lenth-6-$n).date("ymd",$time).$num;
}
}
}
ASP函数:
Function DanNum(DanNo,Num)
Dim Dan,Begin,No,i
If Num=0 Then
Dan=""
Else
No=Clng(Right(DanNo,Num))+1
For i=1 to Num
If Len(No)=i then
Dan=String(Clng(Num-i),"0")&No '生成后面的随机数
End If
Next
End If
If Clng(Left(Right(DanNo,Num+2),2))<>Clng(Day(date)) or Clng(Left(Right(DanNo,Num+4),2))<>Clng(Month(date)) or Clng(Left(Right(DanNo,Num+6),2))<>Clng(Right(Year(date),2)) then
Dim Nian,Yue,Ri
Nian=Right(Year(Date),2)
If Len(Month(date))=1 Then
Yue="0"&Month(Date)
Else
Yue=Month(Date)
End If
If Len(Day(Date))=1 Then
Ri="0"&Day(Date)
Else
Ri=Day(Date)
End If
If Num=0 Then
Dan=""
Else
Dan=String(Clng(Num-1),"0")&"1"
End If
DanNum=Left(DanNo,Len(DanNo)-Num-6)&Nian&Yue&Ri&Dan
Else
DanNum=Left(DanNo,Len(DanNo)-Num)&Dan
End If
End Function
自我感觉PHP函数好像写得复杂了一些,这算法挺笨的,而ASP函数呢,写得也是够笨的,不过都还能用,各位兄弟有赏光的就先凑合用吧,我改天再升级一下,使用过程中有问题的在下面留言即可^_^