[英]How to create 10 digit random number with current date using PHP
I need to create 10 digit random number with current date and some input value using PHP. 我需要使用PHP创建具有当前日期的10位随机数和一些输入值。 I am explaining my code below. 我在下面解释我的代码。
$current_date=date('Y-m-d');//2018-04-30
$user_input=1;
above data are the user input and my expected output should look below. 以上数据是用户输入,我的预期输出应在下面。
$output=1804300001
The random number generation format should be like this yymmdd
with 4 digit number including user input
. 随机数生成格式应类似于yymmdd
, 4 digit number including user input
。 Suppose user_input=1 then it should be 0001
and user_input=12
then it should be 0012
like this. 假设user_input = 1则应为0001
而user_input=12
则应为0012
如下所示。
Something like this 像这样
$date = new DateTime();
$input = 1;
$output = date_format($date,"ymd").sprintf('%04u', $input);
<?php
echo getToken(1); #201804300001
echo getToken(9999); #201804309999
echo getToken(12345); #201804301234
function getToken($input) {
return date('Ymd').str_pad (substr($input, 0, 4) , 4, '0', STR_PAD_LEFT);
}
here some code for you...there many solutions. 这里有一些适合您的代码...有很多解决方案。
$user = 12;
$date = date("ymd", $time());
echo $datum .sprintf("%04u",$user);
Best regards 最好的祝福
If using your code ( $current_date=date('Ym-d'); //2018-04-30
): 如果使用您的代码( $current_date=date('Ym-d'); //2018-04-30
):
$current_date = date('Y-m-d');
$user_input = 1;
$result = substr(str_replace('-', '', $current_date), 2); // Replace "-" with nothing plus remove first 2 digits
$result .= str_pad($user_input, 4, '0', STR_PAD_LEFT); // Fill in with "0" to the left
Might not be the prettiest code but it works fine. 可能不是最漂亮的代码,但效果很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.