首页 > php教程 > php手册 > PHP学习笔记:利用时间和mt_rand函数获取随机名字

PHP学习笔记:利用时间和mt_rand函数获取随机名字

WBOY
发布: 2016-09-20 03:30:22
原创
1195 人浏览过

这个知识会在文件上传等场合用到,还没学面向对象,现在用函数形式呈献给各位,代码都做了备注,有不懂得可以在线提问。

<span style="font-size: 18px;"><span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/19 0019
 * Time: 19:47
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span><span style="color: #000000;"> rdname(){
    </span><span style="color: #800080;">$t</span> = <span style="color: #008080;">getdate</span><span style="color: #000000;">();
    </span><span style="color: #800080;">$year</span>=<span style="color: #800080;">$t</span>['year'<span style="color: #000000;">];
    </span><span style="color: #800080;">$month</span> = <span style="color: #800080;">$t</span>['mon']$t</span>['mon']:<span style="color: #800080;">$t</span>['mon'<span style="color: #000000;">];
    </span><span style="color: #800080;">$day</span> = <span style="color: #800080;">$t</span>['mday']>9?<span style="color: #800080;">$t</span>['mday']:"0".<span style="color: #800080;">$t</span>['mday'<span style="color: #000000;">];
    </span><span style="color: #800080;">$hour</span>= <span style="color: #800080;">$t</span>['hours']>9?<span style="color: #800080;">$t</span>['hours']:"0".<span style="color: #800080;">$t</span>['hours'<span style="color: #000000;">];
    </span><span style="color: #800080;">$min</span> = <span style="color: #800080;">$t</span>['minutes']>9?<span style="color: #800080;">$t</span>['minutes']:"0".<span style="color: #800080;">$t</span>['minutes'<span style="color: #000000;">];
    </span><span style="color: #800080;">$sec</span> = <span style="color: #800080;">$t</span>['seconds']>9?<span style="color: #800080;">$t</span>['seconds']:"0".<span style="color: #800080;">$t</span>['seconds'<span style="color: #000000;">];
    </span><span style="color: #800080;">$randnum</span>=<span style="color: #008080;">mt_rand</span>(1000,9999);<span style="color: #008000;">//</span><span style="color: #008000;">获取随机的4个数字</span>
    <span style="color: #800080;">$randname</span>=<span style="color: #800080;">$year</span>.<span style="color: #800080;">$month</span>.<span style="color: #800080;">$day</span>.<span style="color: #800080;">$hour</span>.<span style="color: #800080;">$min</span>.<span style="color: #800080;">$sec</span>.<span style="color: #800080;">$randnum</span>;<span style="color: #008000;">//</span><span style="color: #008000;">把年月日时分和随机数拼接起来</span>
   <span style="color: #0000ff;">return</span> <span style="color: #800080;">$randname</span>;<span style="color: #008000;">//</span><span style="color: #008000;">返回新的名字</span>
<span style="color: #000000;">}
</span><span style="color: #800080;">$newname</span>=<span style="color: #000000;">rdname();
</span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$newname</span><span style="color: #000000;">;
</span>?><span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/19 0019
 * Time: 19:47
 </span><span style="color: #008000;">*/</span>
<span style="color: #0000ff;">function</span><span style="color: #000000;"> rdname(){
    </span><span style="color: #800080;">$t</span> = <span style="color: #008080;">getdate</span><span style="color: #000000;">();
    </span><span style="color: #800080;">$year</span>=<span style="color: #800080;">$t</span>['year'<span style="color: #000000;">];
    </span><span style="color: #800080;">$month</span> = <span style="color: #800080;">$t</span>['mon']$t['mon']:<span style="color: #800080;">$t</span>['mon'<span style="color: #000000;">];
    </span><span style="color: #800080;">$day</span> = <span style="color: #800080;">$t</span>['mday']>9?<span style="color: #800080;">$t</span>['mday']:"0".<span style="color: #800080;">$t</span>['mday'<span style="color: #000000;">];
    </span><span style="color: #800080;">$hour</span>= <span style="color: #800080;">$t</span>['hours']>9?<span style="color: #800080;">$t</span>['hours']:"0".<span style="color: #800080;">$t</span>['hours'<span style="color: #000000;">];
    </span><span style="color: #800080;">$min</span> = <span style="color: #800080;">$t</span>['minutes']>9?<span style="color: #800080;">$t</span>['minutes']:"0".<span style="color: #800080;">$t</span>['minutes'<span style="color: #000000;">];
    </span><span style="color: #800080;">$sec</span> = <span style="color: #800080;">$t</span>['seconds']>9?<span style="color: #800080;">$t</span>['seconds']:"0".<span style="color: #800080;">$t</span>['seconds'<span style="color: #000000;">];
    </span><span style="color: #800080;">$randnum</span>=<span style="color: #008080;">mt_rand</span>(1000,9999);<span style="color: #008000;">//</span><span style="color: #008000;">获取随机的4个数字</span>
    <span style="color: #800080;">$randname</span>=<span style="color: #800080;">$year</span>.<span style="color: #800080;">$month</span>.<span style="color: #800080;">$day</span>.<span style="color: #800080;">$hour</span>.<span style="color: #800080;">$min</span>.<span style="color: #800080;">$sec</span>.<span style="color: #800080;">$randnum</span>;<span style="color: #008000;">//</span><span style="color: #008000;">把年月日时分和随机数拼接起来</span>
   <span style="color: #0000ff;">return</span> <span style="color: #800080;">$randname</span>;<span style="color: #008000;">//</span><span style="color: #008000;">返回新的名字</span>
<span style="color: #000000;">}
</span><span style="color: #800080;">$newname</span>=<span style="color: #000000;">rdname();//把返回的名字赋值给</span>$newname
登录后复制
<span style="font-size: 18px;"><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$newname</span><span style="color: #000000;">; </span>?></span>
登录后复制

效果展示:

来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板