Creating a unique number from a time stamp PHP


What would be the best PHP function to use in order to create a unique reference up to 20 characters based on a time stamp.

thanks in advance,
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Does it have to be 20, can it not be 32 characters long?

As you could create the md5 hash of a timestamp, should be unique unless two get generated at exactly the same time.
The function time() "Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).", aka a large integer of 10 places. How precise do you need it? This one obviously won't work if you need finely granularity than seconds on the unique ids.... but it works just fine in situations like I use it: measure temperatures every two minutes.
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

You would have to check both are unique though, if two are generated at the same time, they will be the same.

Could you not store a new record in the database each time and take the primary key, they would ensure they are always unique?
$hash = md5(time());

// Would generate 32 char hash.

$hash = time().time();

// Would generate 20.

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
As far as I can tell, you need two pieces, a random bit and the timestamp, if you want it to be based on timestamp + unique with finer than second granularity.

Akin to the unix command date '+%s%N', using a timestamp + a random number should do the trick:

srand ((double) microtime( )*10000000000);
$random_number = rand( );
$uniqueID = time().$random_number;

// or the lazy way:
$uniqueID = exec("date '+%s%N'");

Open in new window

Julian HansenCommented:
You can try uniqueid() (value of true extends the length

To get to 20 chars you can take the least significant 20 bytes.

*microtime( )*10000000000 should be microtime( )*1000000. Too many 0s.....

If you want alpha-numeric characters, uniqid will work.
danzzzAuthor Commented:
Thanks for all the answers.

Solution by marklogan did the job perfectly since it turns out the length has to be exactly 20 characters.

Many thanks,
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.