doutangdan3588 2014-07-07 10:52
浏览 86

任何特定月份的PHP代码生成从00001到99999

I am using this php code to generate a code

<?php 
$year = date('y'); //gets 2 digits of year, ex, year 2014 as 14
$month = date('m');//gets 2 digits of month, ex, month July as 07
$day = date('d');//gets 2 digits of date, ex, date 07
$random = substr( md5(rand()), 0, 4); //generate 4 digits random number using 0-9 and a-z
$code = $year . $month . $day . $random;
echo $code;
?>

Next I am trying to add a 5 digit number to this code starting from 00001 to 99999

means the code shall look like, YYMMDDXXXXNNNNN while I have achieved YYMMDDXXXX, I am still struggling with NNNNN part (00000-99999).

How can that be made and concatenated to this variable $code with each user generate entry.

I want to mention here, this is the code number of a form, when 1st user fills the form on any date the NNNNN given to him would be 00001, for 2nd user 00002, 3rd user 00003 and so on, irrespective of any date in a particular month.

Also, When the month is changed from current to next, I want that NNNNN again to be reset to 00001.

How can I achieve those two things.

Any idea guys?

here is the snippet http://codepad.org/cgPLEtr7

  • 写回答

2条回答 默认 最新

  • duangu4980 2014-07-07 11:10
    关注

    You can use:

    $form = sprintf("%05s", 1);
    $code = $year . $month . $day . $random . $form;
    

    to get the value of $code right.

    Below are a few more hints about your 'other' queries.

    You could try setting up another MySQL table to store your 5-digit form number, which will initially be '00000', as a varchar.

    Then, on data insert to your original table, trigger MySQL to update this value by +1 after a cast, as in: https://stackoverflow.com/a/6873429/3318560

    Draw this value to your form via PHP, and keep it there as a hidden value that you will append to your $code.

    Finally, you could use a MySQL event to reset your value to 00000 each month.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?