Php unique number based on timestamp

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?

asked Aug 28, 2012 at 6:33

6

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[];

answered Aug 28, 2012 at 6:42

amitchhajeramitchhajer

12.1k6 gold badges39 silver badges52 bronze badges

0

You can use a combination of uniqid[] and time[] like so:

$s = uniqid[time[], true];

Example output:

1346136883503c6b3330caf5.19553126

answered Aug 28, 2012 at 6:55

Ja͢ckJa͢ck

168k36 gold badges255 silver badges305 bronze badges

1

Is this what you're looking for? uniqid[]

From the doc:

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

answered Aug 28, 2012 at 6:34

Lucas GreenLucas Green

3,90120 silver badges26 bronze badges

7

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    The uniqid[] function in PHP is an inbuilt function which is used to generate a unique ID based on the current time in microseconds [micro time]. 
    The ID generated from the uniqid[] function is not optimal since it is based on the system time and is not cryptographically secured. Thus it should not be for cryptographical purposes.
    The uniqid[ ] function accepts prefix and more_entropy as parameters and returns timestamp based unique identifier as a string.
     

    Syntax:  

     uniqid[$prefix, $more_entropy] 

    Parameters Used: The uiqid[] function in PHP accepts two parameters. 

    1. $prefix : It is an optional parameter which specifies a prefix to the unique id. It must be string.
    2. $more_entropy : It is an optional parameter which specifies more entropy at the end of the return value which makes the id more unique.The default value is FALSE, which returns 13 characters long string whereas when it is set to TRUE, the return string is 23 characters long.

    Return Value: It returns timestamp based unique identifier as a string.
    Errors And Exceptions: 

    1. The uniqid[] function tries to create unique identifier, but it does not guarantee 100% uniqueness of return value.
    2. Since most systems adjust system clock by NTP or like, system time is changed constantly. Therefore, it is possible that this function does not return unique ID for the process/thread.

    Below programs illustrate the uniqid[] function:
    Program 1: 

    php

    Output:  

    3b2c662647f18

    Program 2: 

    php

    Output:  

    gfg5b2b451823970

    Program 3: 

    php

    Output:  

    gfg5b2b4555ab6bd7.27884925

    Reference : //php.net/manual/en/function.uniqid.php


    Chủ Đề