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

3

5

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.

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

Christiachristian answered 28/8, 2012 at 6:33 Comment(9)
use time stamp, its always uniqueVegetative
codepad.org/LGM4MjX2 , timestampsVegetative
@Vegetative are you sure there won't be any conflict if i run the code any number of times and any day?Christiachristian
check answer, add mt_rand() if you need to make sure about random values, time should do the work too.Vegetative
Oh my God... How can Unix timestamps with a 1-second resolution be unique?Loquitur
why you guys think the time stamp won't be unique (not saying you are wrong), so same time in a timezone will never come again ,so a unix time stamp for that time is generated only once, right?Vegetative
@Vegetative Call same function 5 times in 0.0002s. Unix timestamp will be the same, as it's same second. Also run 2 processes at exact same time (e.g. 2 parallel requests) - timestamp is for all processes. You are very wrong.Iatrochemistry
@Vegetative Same your example, just with more calls: codepad.org/Isj2lCndIatrochemistry
agreed, I was so wrong!Vegetative
V
8

Using time() to create sortable unique id's

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

$uniqueId= time().'-'.mt_rand();
Vegetative answered 28/8, 2012 at 6:42 Comment(0)
F
4

With PHP 7 you can use this to generate a sufficiently random value based on the current timestamp:

$s = time() . bin2hex(random_bytes(10));

Do note that after 265 years (starting with 11 digits in 2286) the timestamp would get another digit, and sorting order will be off; if your code is expected to survive for that long, you may want to consider some padding.

Old answer

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

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

Example output:

1346136883503c6b3330caf5.19553126
Fathometer answered 28/8, 2012 at 6:55 Comment(2)
We can also complexify the generation of a unique id with the function md5 md5(uniqid(time(), true)).Eijkman
"does not guarantee uniquenes" ; "Use more_entropy to increase likelihood of uniqueness". So it's not really unique, but the conflict will be less probable.Endive
E
1

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

From the doc:

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

Eweneck answered 28/8, 2012 at 6:34 Comment(7)
will it generate uniqueid() based on time only?? if so what happens if there is a time conflict...i am not clear.. i need a manual code that generates unique id every time i run the code @MythrilChristiachristian
You'll possibly get the same ID if your IDs are generated on the same microsecond. It's difficult but could happen. You can make it harder by appending a random string--the base technique is good IMHO.Loquitur
php.net/manual/en/function.uniqid.php the doc indicates that you can provide a prefix and an entropy source.Eweneck
what do they mean by prefix string? I dont get it.. entropy too.. @MythrilChristiachristian
A prefix is an arbitrary string that you provide to give the id extra meaning/differentiate it (it's up to you really). An entropy source is just an additional number (preferably random or pseudo-random) that will make the id less likely to collide with previously generated ids.Eweneck
have you tried 'time().'-'.mt_rand();'.. can you ensure uniqid() gives a better result than this? i mean what is the best way.. no offence.Christiachristian
do you want to store Id's in sorted order?Vegetative

© 2022 - 2024 — McMap. All rights reserved.