PHP学习笔记:利用时间和mt_rand函数获取随机名字
程序员文章站
2022-05-24 14:12:13
...
这个知识会在文件上传等场合用到,还没学面向对象,现在用函数形式呈献给各位,代码都做了备注,有不懂得可以在线提问。
php /** * Created by PhpStorm. * User: Administrator * Date: 2016/9/19 0019 * Time: 19:47 */ function rdname(){ $t = getdate(); $year=$t['year']; $month = $t['mon']$t['mon']:$t['mon']; $day = $t['mday']>9?$t['mday']:"0".$t['mday']; $hour= $t['hours']>9?$t['hours']:"0".$t['hours']; $min = $t['minutes']>9?$t['minutes']:"0".$t['minutes']; $sec = $t['seconds']>9?$t['seconds']:"0".$t['seconds']; $randnum=mt_rand(1000,9999);//获取随机的4个数字 $randname=$year.$month.$day.$hour.$min.$sec.$randnum;//把年月日时分和随机数拼接起来 return $randname;//返回新的名字 } $newname=rdname(); echo $newname; ?>php /** * Created by PhpStorm. * User: Administrator * Date: 2016/9/19 0019 * Time: 19:47 */ function rdname(){ $t = getdate(); $year=$t['year']; $month = $t['mon']$t['mon']:$t['mon']; $day = $t['mday']>9?$t['mday']:"0".$t['mday']; $hour= $t['hours']>9?$t['hours']:"0".$t['hours']; $min = $t['minutes']>9?$t['minutes']:"0".$t['minutes']; $sec = $t['seconds']>9?$t['seconds']:"0".$t['seconds']; $randnum=mt_rand(1000,9999);//获取随机的4个数字 $randname=$year.$month.$day.$hour.$min.$sec.$randnum;//把年月日时分和随机数拼接起来 return $randname;//返回新的名字 } $newname=rdname();//把返回的名字赋值给$newname
echo $newname; ?>
效果展示: