PHP 实现签到功能
如图所示:
实现上图功能,创建两张表:签到奖品表、签到记录表(这个表可以再拆分出一个表)
1 /** 2 *功能:计算两个时间戳之间相差的日时分秒 3 *$begin_time 开始时间戳 4 *$end_time 结束时间戳 5 */ 6 public function timediff($begin_time,$end_time){ 7 if($begin_time < $end_time){ 8 $starttime = $begin_time; 9 $endtime = $end_time; 10 }else{ 11 $starttime = $end_time; 12 $endtime = $begin_time; 13 } 14 15 //计算天数 16 $timediff = $endtime-$starttime; 17 $days = intval($timediff/86400); 18 //计算小时数 19 $remain = $timediff%86400; 20 $hours = intval($remain/3600); 21 //计算分钟数 22 $remain = $remain%3600; 23 $mins = intval($remain/60); 24 //计算秒数 25 $secs = $remain%60; 26 $res = array("day" => $days,"hour" => $hours,"min" => $mins,"sec" => $secs); 27 return $res; 28 } 29 30 //黄梅--签到列表 20171206 by csj 31 public function signList(){ 32 $user_id = I("param.user_id","635747","trim"); 33 34 if (!($user_id)) { 35 $r['message'] = '缺少参数'; 36 GResult::getInstance()->echoErroAndMessage($r);exit(); 37 } 38 $list = D('loginrewards')->field('id,day,reward,icon,type,name,des')->select(); 39 $isOk = D('sign')->where("user_id={$user_id}")->order('id desc')->find(); 40 41 $last_time = strtotime($isOk['s_time']);//最后一次签到时间 42 43 $t = time(); 44 // $start = mktime(0,0,0,date("m",$t),date("d",$t),date("Y",$t)); 45 $end = mktime(23,59,59,date("m",$t),date("d",$t),date("Y",$t));//当天时间的最后1刻 (注意) 46 47 $current_day = $end;//当天时间的最后1刻 48 49 $timeIs = $this->timediff($last_time,$current_day);//时间差 50 51 if (!empty($isOk)) { 52 foreach ($list as $key => $value) { 53 //判断是否漏签 54 if ($timeIs['day']>1) {// 漏签 55 if ($value['day']==1) { 56 $list[$key]['is_sign'] = 1;//仅第一天的可以签 即首次签到 57 }else{ 58 $list[$key]['is_sign'] = 2;//不可以签 59 } 60 }else{// 未漏签 61 if ($isOk['s_number']==7) {//签满7天后 62 if ($timeIs['day']==1) {//今天没签 63 if ($value['day']==1) { 64 $is_sign = 1;//仅第一天的可以签 即首次签到 65 }else{ 66 $is_sign = 2;//不可以签 67 } 68 }elseif ($timeIs['day']==0) {//今天签过 69 $is_sign = 3;//已签到过 70 } 71 72 }else{//未签满7天,如1-6天之间 73 74 if ($timeIs['day']==1) {//今天没签 75 if ($value['day']==$isOk['s_number']+1) { 76 $is_sign = 1;//可以签 77 }elseif($value['day']<=$isOk['s_number']){ 78 $is_sign = 3;//已签到过 79 }else{ 80 $is_sign = 2;//不可以签 81 } 82 }elseif ($timeIs['day']==0) {//今天签过 83 84 if($value['day']<=$isOk['s_number']){ 85 $is_sign = 3;//已签到过 86 }else{ 87 $is_sign = 2;//不可以签 88 } 89 } 90 } 91 $list[$key]['is_sign'] = $is_sign; 92 } 93 } 94 }else{//首次签到 95 foreach ($list as $k => $v) { 96 if ($v['day']==1) { 97 $list[$k]['is_sign'] = 1;//仅第一天的可以签 即首次签到 98 }else{ 99 $list[$k]['is_sign'] = 2;//不可以签 100 } 101 } 102 } 103 $re['data'] = $list; 104 $re['message'] = '请求成功'; 105 GResult::getInstance()->echoOkAndResult($re); 106 }
参考:https://www.cnblogs.com/gaoxiaoniu/p/5870394.html