Unique identifier with time ()

If I use:

$t = time();
echo $t;

This will output something like: 1319390934

I have two questions:

  • Can this value be used as a unique identifier?
  • How to generate a date from it?

I can’t use uniqid()because I need a value that can be used to order (recent).

+5
source share
3 answers

Using time(), as mentioned, will give you the ability to sort unique identifiers. Concatenating strings will also randomize your desired result and keep it sorted:

$uniqueId= time().'-'.mt_rand();
+9
source

-, , , - , uniqid:

$u = time().'-'.uniqid(true);

, , , , time , ( , ). ,

$u = sprintf("%010s-%s", time(), uniqid(true));
+4

If you use this code in an environment where you have a user account with a unique identifier, you can add time () to their account identifier to create a unique identifier.

You can turn the time () back to the date string using:

$time = time();
echo 'The datestamp for (' . $time . ') is ' . date("Y-m-d", $time);

Of course, the date format can be changed using any PHP date () format.

+1
source

All Articles