# How to generate an integer unque id that is made up of a current timestamp and a random 5 digit number

Hi guys!

How to generate an integer unique id that is made up of a current timestamp and a random 5 digit number.

For example 159655053112345

I tried this:

\$OrderID = (int)date(‘Ymd’).substr(time(), -5) . substr(microtime(), 2, 5) . sprintf(‘%02d’, rand(1000, 9999));

But I got the same id every time.
I get to save it to an integer filed of MySQL.

Well, i would encourage you to use a more unique method of generating uniqueness, but, so be it.

The current timestamp is just `time()`. Your random 5 digit number cant be between 1000 and 9999, because that’s only 4 digits

I would also be tempted to separate and var_dump(…) the time and random number parts. Adding strings and integers give strange results.

I did this:

``````\$OrderID = (int)(date('Ymd') . rand(1, 100));
var_dump(\$OrderID);
``````

It generated an integer like this: 202008056
But, it is too short for me (maybe generates some duplicated ids).

I tried to change it to :

``````\$OrderID = (int)(date('Ymd') . rand(10000, 99999));
var_dump(\$OrderID);
``````

But, every time it gave me the same number:
`int(2147483647)`

Based on this article, I understand the ‘2147483647’ is the largest number of int.

I also changed my mysql data type to the ‘bigin’, but it still gave me the ‘2147483647’ stuff.

How to generate a longer ‘date + random number’ integer?

Without any explanation @igor_g your links aren’t a lot of help to the OP.

I think what you’re TRYING to do is get the timestamp of midnight in milliseconds and then fill in the zeroes. I still think you’re going about the idea oddly, but sure.

`strtotime('today')*1000+rand(10000,99999)`

(This effectively generates a random timestamp between midnight and 12:01:39.999, today)

It seems to be working fine, thank you!

I will simply use shuffle()

``````\$time = strtotime('today');

\$my = str_shuffle(\$time);
echo \$my;
``````

There are more ways to generate a unique code.

1. Use the above method if you must use current time.

2. Create more variables or a rand() and concatenate them.

3. Use rand with multiplication and a substr()

This method will throw you off balance because you will be getting scanty figures
Supposed you want a fixed unique 16 digits this method will give unstable digits and if the value exceeds 32 or so it will return an E+67 kind of count

this method will generate a number exactly 13 digits long for the next 2 and a half centuries (Specifically, until November 19, 2286 at 2:00:00 PM GMT.) After that, it will generate a number that is either 13 or 14 digits long until the next day at 5:46:40 PM, after which it will generate numbers exactly 14 digits long.

the timestamp string is exactly 10 digits long; multiplying it by 1000 adds 3 digits. Adding a number between 10000 and 99999 to this number does not change the length of the number - It’s not APPENDING, it’s ADDING.

In a 64 bit system, PHP’s integer type can store a value equal to 9223372036854775807 - far bigger than the timestamp.

2 Likes

@m_hutley we are all here at sitepoint but you left us here and travel half a century and returned, hope you saw my great, great, great grand kids? Hope they look cute.

I have always known your fingers are gifted in coding, perfectly analogy.

But i rather stick with shuffle to save Apache from doing bytes calculations, and more over shuffle stays till forever but yours has an expiring date.

What do you think?

mine does what the OP asked for. A datetime followed by a random digit 1-9 and 4 random digits 0-9.

I didnt say it was the most random. In fact, I made the statement that there are signifcantly more random random-strings that could be generated.

But it is meaningful. It’s a way of attempting to add entropy to a static timestamp; to avoid a collision (or at least, make the collision condition change from “placed on the same day” to “placed on the same day and 1-in-90000 chance.”

Hi OP, what is the purpose of this? This sounds like an attempted solution to another problem. What is the high level overview of what you have going on?

@m_hutley i know you always try to answer question exactly the way the user asked it and thats professional enough,

But i think at this time the place of strtotime is of no use, getting a time and multiplying and adding rand so whats the place of time stamp? What he needs is a straight random method

``````\$num = substr(str_shuffle('12345678901234567890'), -13);

echo \$num;
``````

Really? Are you sure he doesnt need to be able to tell the time of the order still? Do you have some great mind-reading capability?

At least benanamen asks the OP, rather than inventing his own decision of what the OP wants.

Regardless, the OP’s original question has been answered; if they wish to come back and change their decision of what they want, so be it, but I’m done with this thread.

2 Likes

That isn’t very random, as each digit can only appear at most twice.

2 Likes

It is easy enough to answer the question as posted, but experience tells me this is really an XY problem (very common on forums) and is not really what the OP needs an answer too. What we need first is for the OP to tell us what he has going, then we can tell him what he needs to solve the real problem which I am quite sure is not “a unique id made up of a current timestamp”.

So OP, I am still waiting to hear from you with a high level overview of what you are doing and what the real problem is you are trying to solve with your attempted solution to that problem.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.