Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unique id consisting of only numbers

Tags:

php

unique

I need to get time based unique id which would only consist of numbers, i was thinking to simply use something like this:

str_replace(".", "", microtime(true))

I would use this id for many things, for example comments posting, and i'm expecting quite high traffic so i would like to know how high are the chances of collision with microtime function?

And perhaps there is a better way to get numerical unique id? Just remember that it has to based on time so sorting could be done.

like image 427
Linas Avatar asked Oct 17 '25 08:10

Linas


1 Answers

you can use this to get uniqid but is not numberic:

$id = uniqid(); //58aea16085f6b

use this to convert it to only numbers:

$id = hexdec( uniqid() ); //1560112880181100

if it's too long use this but maybe get negetive numbers:

$id = crc32( uniqid() ); //-1551585806

if you just need posetive numbers you can do it:

$id = abs( crc32( uniqid() ) ); //1551585806
like image 136
Amir Akef Avatar answered Oct 19 '25 22:10

Amir Akef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!