CREATE TABLE `sp_sign` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵',
`times` datetime DEFAULT NULL COMMENT '最近一次簽到時(shí)間',
`userid` int(11) DEFAULT NULL COMMENT '用戶id',
`days` tinyint(6) NOT NULL DEFAULT '0' COMMENT '連續(xù)簽到的天數(shù)',
`number` decimal(10,0) NOT NULL DEFAULT '0' COMMENT '當(dāng)月簽到給的積分',
`one` varchar(255) DEFAULT NULL COMMENT '當(dāng)月簽到的日期,用“,”隔開',
`two` varchar(255) DEFAULT NULL COMMENT '上個(gè)月簽到的日期,用“,”隔開',
`three` varchar(255) DEFAULT NULL COMMENT '上上個(gè)月簽到的日期,用“,”隔開',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;
/**
* 用戶簽到
* @param array $userid 用戶id
*/
public function add($userid)
{
$data = Db::name('sign')->where('userid',$userid)->select();
if(count($data) == 0) //沒有該用戶的簽到記錄
{
$query4 = Db::name('sign')->insert(['times'=>date('Y-m-d H:i:s'),'userid'=>$userid,'days'=>1,'number'=>'15','one'=>date('d',time())]);
return 1;
}
else
{
//判斷今天是否簽到
$todayBegin=date('Y-m-d'." 00:00:00");
$todayEnd= date('Y-m-d'." 23:59:59");
$isexit = Db::name('sign')->field('times')->where(['userid'=>$userid])->where('times','between',[$todayBegin,$todayEnd])->select();
if(count($isexit) == 1) //今日已簽到
{
return 0;
}
else //今日未簽到
{
$times = Db::name('sign')->where('userid',$userid)->field('times')->select();
$time = strtotime($times[0]['times']);
if((time()-$time > 24*60*60)) //上次簽到時(shí)間大于24小時(shí),連續(xù)簽到天數(shù)清零
{
$query = Db::name('sign')->where('userid',$userid)->update(['days'=>1]);
}
else //上次簽到時(shí)間小于24小時(shí),連續(xù)簽到次數(shù)加1
{
$query = Db::name('sign')->where('userid',$userid)->setInc('days');
}
//更新上次簽到時(shí)間和簽到積分
$query1 = Db::name('sign')->where('userid',$userid)->update(['times'=>date('Y-m-d H:i:s')]);
$query2 = Db::name('sign')->where('userid',$userid)->setInc('number', 15);
$sqldate = date('m',$time); //上次簽到日期的月份
$nowdate = date('m',time()); //當(dāng)前月份
//記錄本次簽到日期
if($sqldate != $nowdate) //上次簽到日期與本次簽到日期月份不一樣
{
$oldtime = $times[0]['times'];
$onetime=date("Y-m-d H:i:s", strtotime("-1 month")); //獲取前1個(gè)月的時(shí)間,獲取格式為2016-12-30 13:26:13
$twotime=date("Y-m-d H:i:s", strtotime("-2 month")); //獲取前2個(gè)月的時(shí)間
$threetime=date("Y-m-d H:i:s", strtotime("-3 month")); //獲取前3個(gè)月的時(shí)間
$rs = Db::name('sign')->where('userid',$userid)->field('one,two,three')->select();
if($oldtime $onetime $oldtime >= $twotime) //月份間隔 大于1個(gè)月,小于2個(gè)月
{
$one = date('d',time());
$two = $rs[0]['one'];
$three = $rs[0]['two'];
}
elseif($oldtime $twotime $oldtime >= $threetime) //月份間隔 大于2個(gè)月,小于3個(gè)月
{
$one = date('d',time());
$two = '';
$three = $rs[0]['one'];
}
elseif($oldtime $threetime) //月份間隔 大于3個(gè)月
{
$one = date('d',time());
$two = '';
$three = '';
}
$query3 = Db::name('sign')->where('userid',$userid)->update(['one'=>$one,'two'=>$two,'three'=>$three]);
}
else //上次簽到日期與本次簽到日期月份一樣
{
$one = Db::name('sign')->where('userid',$userid)->field('one')->select();
$arr[] = $one[0]['one'];
$arr[] = date('d',time());
$newones = implode(",",$arr);
$query3 = Db::name('sign')->where('userid',$userid)->update(['one'=>$newones]);
}
return 1;
}
}
}
更多關(guān)于thinkPHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結(jié)》、《ThinkPHP常用方法總結(jié)》、《codeigniter入門教程》、《CI(CodeIgniter)框架進(jìn)階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術(shù)總結(jié)》。