reverbnation reverbnation - 2 years ago 77
PHP Question

can I use strtotime('now') as unique ID in PHP?

Can I use strtotime("now") as a unique ID in php application as I assume it uses current timestamp and cannot generate the same integer in future?

Answer Source

It is not safe to use strtotime('now') as id

take a look at this code:

$now = strtotime('now');
$anotherNow = strtotime('now');
$sameCounter = 0;
while($anotherNow == $now){
    $anotherNow = strtotime('now');

echo $sameCounter; //8558

Result is 8558, very not safe, php did 8558 operations during that second
There's big chance that you'll get the same ID few times.

you can use uniq function instead

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download