I need to create 10 digit random number with current date and some input value using PHP. 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
. Suppose user_input=1 then it should be 0001
and user_input=12
then it should be 0012
like this.
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('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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.