Home  >  Article  >  类库下载  >  Get random names using time and mt_rand function

Get random names using time and mt_rand function

高洛峰
高洛峰Original
2016-10-09 13:11:311381browse

This knowledge will be used in file uploading and other occasions. I haven’t learned object-oriented yet, so now I present it to you in functional form

<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/19 0019
 * Time: 19:47
 */
function rdname(){
    $t = getdate();
    $year=$t[&#39;year&#39;];
    $month = $t[&#39;mon&#39;]<10? "0".$t[&#39;mon&#39;]:$t[&#39;mon&#39;];
    $day = $t[&#39;mday&#39;]>9?$t[&#39;mday&#39;]:"0".$t[&#39;mday&#39;];
    $hour= $t[&#39;hours&#39;]>9?$t[&#39;hours&#39;]:"0".$t[&#39;hours&#39;];
    $min = $t[&#39;minutes&#39;]>9?$t[&#39;minutes&#39;]:"0".$t[&#39;minutes&#39;];
    $sec = $t[&#39;seconds&#39;]>9?$t[&#39;seconds&#39;]:"0".$t[&#39;seconds&#39;];
    $randnum=mt_rand(1000,9999);//获取随机的4个数字
    $randname=$year.$month.$day.$hour.$min.$sec.$randnum;//把年月日时分和随机数拼接起来
   return $randname;//返回新的名字
}
$newname=rdname();//把返回的名字赋值给$newname
echo $newname; ?>

Effect display:

Get random names using time and mt_rand functionGet random names using time and mt_rand function

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more