加载中…
个人资料
神秘小阿飞
神秘小阿飞
  • 博客等级:
  • 博客积分:0
  • 博客访问:487
  • 关注人气:0
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
相关博文
推荐博文
谁看过这篇博文
加载中…
正文 字体大小:

PHP 发红包 把每个红包分配到每个时间点

(2018-03-08 15:21:23)
标签:

php

红包

分散每个

时间


public function doReceiveTmp(){
$bundle_id =53;
$coupon_bundle_db = M("coupon_bundle");
$coupon_db = M("coupon");
$coupon_bundle_where['id'] =$bundle_id;
$bundle_info = $coupon_bundle_db->where($coupon_bundle_where)->find();
$AwardBatchListTmp = $bundle_info['uniqids'];
$coupon_where['uniqid'] = array('in',$AwardBatchListTmp);
$AwardBatchs = $coupon_db->where($coupon_where)->select();

$timelist = json_decode($bundle_info['timeList'],1);
$tmp_timelist="";
$total_time = "";
foreach($timelist as $tk=>$tr){
$time = explode("~",$tr);
$tmp_timelist[$tk]['start_time'] = strtotime($time[0]);
$tmp_timelist[$tk]['end_time'] = strtotime($time[1]);
}
foreach($tmp_timelist as $tmk=>$tmr){
$total_time += $tmr['end_time']-$tmr['start_time'];
}
$now = time();
$startTime = 0;
$endTime = $total_time;
$totalAmount = 0;
$totalBalance=0;
foreach($AwardBatchs as $key=>$row){
$totalBalance += $row['balance'];
$totalAmount += $row['amount'];
}
$AwardBatch = $this->randomGetAwardBatch($AwardBatchs);
$last_update_time =$AwardBatch['last_update_time'];
$currentBalance =$totalBalance;
$currentAmount = $totalAmount;
$dateTime = ($endTime-$startTime)/$currentAmount;
srand($last_update_time);
$tmp = rand();
$relaseTime = $startTime+($currentAmount-$currentBalance)*$dateTime+$tmp%$dateTime;

foreach($tmp_timelist as $ttk=>$ttr){
$times = $ttr['end_time']-$ttr['start_time'];
if($relaseTime>$times){
$relaseTime -=$times;
}else{
$relaseTime += $ttr['start_time'];
}

}

if($now<</span>$relaseTime){
echo "未中";
}else{
echo "中了";
$where['id'] = $AwardBatch['id'];
$coupon_db->where($where)->setDec("balance",1);
unset($where);
foreach($AwardBatchs as $tmpkey=>$tmprow){
$where['id'] = $tmprow['id'];
$save_info['last_update_time'] = time();
$coupon_db->where($where)->save($save_info);
}
}
}



public function randomGetAwardBatch($AwardBatch=array()){
$weight = 0;
foreach($AwardBatch as $key=>$row){
$weight += $row['balance'];
}
if($weight==0){
return null;
}
$num = rand(0,$weight-1);

foreach($AwardBatch as $key=>$row){
$num-= $row['balance'];
if($num<</span>0){
return $row;
}
}
}

0

阅读 评论 收藏 转载 喜欢 打印举报/Report
前一篇:PHP CURL 配置
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

    < 前一篇PHP CURL 配置
      

    新浪BLOG意见反馈留言板 电话:4000520066 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有