Hello, I really need your help. I need to make a unique ID in yii2, I have an example in php

$query->orderBy('v_r.vid', 'DESC'); $query->range(0, 1); $results = $query->execute()->fetchAll(); $new_unique = 'A1A1234A'; if(isset($results[0]->vid)) { $old_id = $results[0]->vid; $num_length = strlen((string)$old_id); switch($num_length) { case 1 : $need_number = 4; break; case 2 : $need_number = 3; break; case 3 : $need_number = 2; break; case 4 : $need_number = 1; break; default : $need_number = 0; break; } $get_a = rand ( 65 , 90 ); $get_b = rand ( 65 , 90 ); $get_c = rand ( 65 , 90 ); $a = chr($get_a); $b = chr($get_b); $c = chr($get_c); $other_numbers = substr(number_format(time() * rand(),0,'',''),0,$need_number); $new_unique = $a.$old_id.$b.$other_numbers.$c; } 

How can this code be used in yii thanks a lot.

    2 answers 2

    You can try using the PHP function md5 () along with the timestamp obtained by the time () function

     $new_unique = md5(time()); 

    Also quite unique

      The question is not quite clear, but if you just need to generate a unique id then:

      http://php.net/manual/ru/function.uniqid.php