Shadow
PHP快速生成类似彩票的开奖号码和生肖
直接上代码
public function createCode()
{
//开始年份
$startYear = 2021;
//结束年份
$endYear = 2022;
$dayList = [];
for ($y = $startYear; $y <= $endYear; $y++) {
for ($m = 1; $m <= 12; $m++) {
$month = $y . '-' . ($m < 10 ? '0' . $m : $m);
$monthDay = date('t', strtotime($month));
for ($d = 1; $d <= $monthDay; $d++) {
$dayList[] = $month . '-' . ($d < 10 ? '0' . $d : $d) . ' 20:30:00';
}
}
}
$prizeNumber = [];
$prizeText = [
'鼠',
'牛',
'虎',
'兔',
'龙',
'蛇',
'马',
'羊',
'猴',
'鸡',
'狗',
'猪',
];
for ($i = 1; $i <= 49; $i++) {
$prizeNumber[] = ($i < 10 ? '0' . $i : $i);
}
$datas = [];
foreach ($dayList as $index => $day) {
$run1 = $prizeNumber;
$run2 = $prizeText;
$createNumber = [];
$createText = [];
for ($n = 0; $n < 7; $n++) {
$maxRand = count($run1) - 1 > 1 ? count($run1) - 1 : 0;
$maxRand2 = count($run2) - 1 > 1 ? count($run2) - 1 : 0;
$index1 = mt_rand(0, $maxRand);
$index2 = mt_rand(0, $maxRand2);
$createNumber[] = $run1[$index1];
$createText[] = $run2[$index2];
array_splice($run1, $index1, 1);
array_splice($run2, $index2, 1);
}
$datas[] = [
'title' => ($index + 1 < 100 ? '00' . ($index + 1) : $index + 1),
'open_time' => $day,
'numbers' => $createNumber,
'texts' => $createText,
'status' => 1,
];
}
$createTime = date('Y-m-d H:i:s');
$sql = 'INSERT INTO `ay_prize` (title,open_time,prize_number1,prize_number2,prize_number3,prize_number4,prize_number5,prize_number6,prize_number7,text_number1,text_number2,text_number3,text_number4,text_number5,text_number6,text_number7,status,create_time,update_time) VALUES ';
foreach ($datas as $data) {
$sql .= "('{$data['title']}','{$data['open_time']}','{$data['numbers'][0]}','{$data['numbers'][1]}','{$data['numbers'][2]}','{$data['numbers'][3]}','{$data['numbers'][4]}','{$data['numbers'][5]}','{$data['numbers'][6]}','{$data['texts'][0]}','{$data['texts'][1]}','{$data['texts'][2]}','{$data['texts'][3]}','{$data['texts'][4]}','{$data['texts'][5]}','{$data['texts'][6]}','{$data['status']}','$createTime','$createTime'),<br/>";
}
echo $sql;
}
Dcr163的博客
https://dcr163.cn/691.html(转载时请注明本文出处及文章链接)