php 如何根据当前数据和时间在php中生成唯一ID?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12153902/
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 02:56:45  来源:igfitidea点击:

how to generate unique id in php based on current data and time?

phpdatedatetimetimeunique-key

提问by user1518659

I need to generate a unique ID in php based on current date and time to keep a log of when i am running code. i.e. every time i run the code it should be able to generate a unique id based on current date and time.

我需要根据当前日期和时间在 php 中生成一个唯一 ID,以记录我何时运行代码。即每次我运行代码时,它应该能够根据当前日期和时间生成一个唯一的 id。

Hope I am clear with my ques. How to do that?

希望我清楚我的问题。怎么做?

回答by amitchhajer

Using time() to create sortable unique id's

使用 time() 创建可排序的唯一 ID

Concatenating strings will also further randomize your desired result and still keep it sortable. manual here

连接字符串还将进一步随机化您想要的结果并仍然保持可排序。手册在这里

$uniqueId= time().'-'.mt_rand();

回答by Ja?ck

You can use a combination of uniqid()and time()like so:

您可以使用组合uniqid()time()像这样:

$s = uniqid(time(), true);

Example output:

示例输出:

1346136883503c6b3330caf5.19553126

回答by Lucas Green

Is this what you're looking for? uniqid()

这是你要找的吗? uniqid()

From the doc:

从文档:

Gets a prefixed unique identifier based on the current time in microseconds.

获取基于当前时间(以微秒为单位)的带前缀的唯一标识符。