如何使用PHP / Codeigniter生成唯一的票号

时间:2017-11-08 09:08:31

标签: php codeigniter

我需要为每个预订添加生成唯一的票号,以生成一个唯一的身份号 例如:B0000000001

3 个答案:

答案 0 :(得分:2)

尝试使用Codeigniter中的random_string

<强>语法

random_string([$type = 'alnum'[, $len = 8]])

可用类型($type

  1. alpha - 仅限小写和大写字母的字符串。
  2. alnum - 包含小写和大写字符的字母数字字符串。
  3. basic - 基于mt_rand()的随机数。
  4. numeric - 数字字符串。
  5. nozero - 没有零的数字字符串。
  6. md5 - 基于md5()的加密随机数(固定长度为32)。
  7. sha1 - 基于sha1()的加密随机数(固定长度为 40)。
  8. Read More about random_string

答案 1 :(得分:0)

试试这个

$uniqueNumber = strftime("%Y%m%d%H%M%S");
每次根据时间生成唯一编号。

答案 2 :(得分:0)

您可以尝试使用str_pad

<强>代码

$max = 9;
for($x = 1; $x <= 11; $x++){

    echo 'B' .str_pad('', $max - strlen((string) $x), '0', STR_PAD_LEFT) . $x . "<br />";
}

<强>结果

B000000001
B000000002
B000000003
B000000004
B000000005
B000000006
B000000007
B000000008
B000000009
B000000010
B000000011
相关问题