php PHP生成带有日期的唯一订单号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22279146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 04:47:31  来源:igfitidea点击:

PHP Generate Unique Order Number with Date

phpdatetimeformat

提问by Ken Tang

I want to generate unique identificator in following 12 numeric format:

我想以以下 12 种数字格式生成唯一标识符:

YYYYMMDDXXXX

Example:

例子:

201403052318

Where:

在哪里:

YYYYMMDD is a current date value and other XXXX is randomly generated value.

YYYYMMDD 是当前日期值,其他 XXXX 是随机生成的值。

$today = date("Ymd");
$rand = sprintf("%04d", rand(0,9999));
$unique = $today . $rand;

Daily required unique volume is about 100. What methods using PHP should I use to prevent possible duplicates in rand values or make all id maximum unique? Maybe possible use current time functions to compact these numbers in last 4 characters?

每日所需的唯一量约为 100。我应该使用什么方法使用 PHP 来防止 rand 值中可能出现重复或使所有 id 最大唯一性?也许可以使用当前时间函数将这些数字压缩为最后 4 个字符?

EDITED: Unique value connected to MySQL database as prime index of table. It is initial values not connected to any stored information in database.

编辑:作为表的主要索引连接到 MySQL 数据库的唯一值。它是与数据库中存储的任何信息无关的初始值。

回答by Shankar Damodaran

You can't rely on rand(), There is a possibility you will generate a duplicate (Pretty rare for a rand(0,9999)to generate a duplicate, but that will at some point).

您不能依赖rand(),您有可能会生成一个副本(生成副本的情况很少见rand(0,9999),但在某些时候会这样)。

So instead of going for rand(), just create an incremental value (say.. starting from 1) and append to your generated date.

因此,与其选择rand(),只需创建一个增量值(例如.. 从 1 开始)并附加到您生成的日期。

Next time when you regenerate a new id, grab that incremental value's (say if you had stored it somewhere.. must be 1 right ?) status, increment it and append it to your new date.

下次当您重新生成新 id 时,获取该增量值的(假设您已将其存储在某处.. 必须是 1 对吗?)状态,将其增加并将其附加到您的新日期。

Not an ideal solution.. (Critics welcome)

不是一个理想的解决方案..(欢迎批评)

You can make use of a uniqidcoupled with sha-1and timeand do a substr()on them for first 4 chars.

您可以使用 auniqid加上sha-1andtime并对substr()前 4 个字符执行 a 。

<?php
$today = date("Ymd");
$rand = strtoupper(substr(uniqid(sha1(time())),0,4));
echo $unique = $today . $rand;

OUTPUT :

OUTPUT :

201403094B3F

回答by Baig

I needed to do something similar, a solution that would keep time and also keep the id unique and i ended up with a solution to use PHPfunctiontime()like this

我需要做同样的事情,那将保持时间,也保持ID唯一一个解决方案,我结束了一个解决方案,使用PHP功能time()类似这样的

$reference_number = 'BFF-' . time(); you can change the BFFto something that makes more sense to your business logic.

$reference_number = 'BFF-' . time(); 您可以将 更改为BFF对您的业务逻辑更有意义的内容。

My unique reference id looks like BFF-1393327176and the number can be converted from Unix to real time which will give you, Tue, 25 Feb 2014 11:19:36

我的唯一参考 ID 看起来像,BFF-1393327176并且该数字可以从 Unix 转换为实时,这将为您提供,Tue, 25 Feb 2014 11:19:36

I hope this helps

我希望这有帮助

回答by SaidbakR

If the unique values generated once, you just need to make conditional choice for the rand value and store the value in an array which is going to be the condition -using inarray-:

如果唯一值生成一次,您只需要对 rand 值进行条件选择并将该值存储在将成为条件的数组中 -使用inarray-:

$amount = 100; // the amount of ids
$previousValues = array();
for ($i = 0; $i < $amount; $i++){    
    $rand = rand(0,9999);
    while (in_array($rand, $previousValues)){
        $rand = rand(0, 9999);
    }
    $previousValues[] = $rand;
    $today = date("Ymd");
    $unique = $today.$rand;
    echo $unique."\n";
}

Checkout this demo.

结帐this demo