TP5框架實(shí)現(xiàn)簽到功能的方法分析
本文實(shí)例講述了TP5框架實(shí)現(xiàn)簽到功能的方法。分享給大家供大家參考,具體如下:
基于tp5 模型的一個(gè)簽到功能;
由于存儲(chǔ)所有的簽到日期數(shù)據(jù)庫(kù)會(huì)非常龐大,所以簽到日期只存儲(chǔ)近三個(gè)月的。
具體功能:
1、記錄最近一次的簽到時(shí)間
2、每次簽到都會(huì)添加15積分
3、有連續(xù)簽到的記錄
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é)》。
希望本文所述對(duì)大家基于ThinkPHP框架的PHP程序設(shè)計(jì)有所幫助。
- thinkPHP實(shí)現(xiàn)簽到功能的方法
- tp5(thinkPHP5)框架數(shù)據(jù)庫(kù)Db增刪改查常見操作總結(jié)
- tp5(thinkPHP5)框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)查詢的方法
- thinkPHP5實(shí)現(xiàn)數(shù)據(jù)庫(kù)添加內(nèi)容的方法
- tp5(thinkPHP5)框架連接數(shù)據(jù)庫(kù)的方法示例
- thinkPHP5框架數(shù)據(jù)庫(kù)連貫操作之cache()用法分析
- thinkPHP5框架實(shí)現(xiàn)多數(shù)據(jù)庫(kù)連接,跨數(shù)據(jù)連接查詢操作示例
- Thinkphp5框架實(shí)現(xiàn)獲取數(shù)據(jù)庫(kù)數(shù)據(jù)到視圖的方法
- ThinkPHP5.1框架數(shù)據(jù)庫(kù)鏈接和增刪改查操作示例
- PHP利用pdo_odbc實(shí)現(xiàn)連接數(shù)據(jù)庫(kù)示例【基于ThinkPHP5.1搭建的項(xiàng)目】
- 基于ThinkPHP5框架使用QueryList爬取并存入mysql數(shù)據(jù)庫(kù)操作示例
- ThinkPHP5.0框架實(shí)現(xiàn)切換數(shù)據(jù)庫(kù)的方法分析
相關(guān)文章
Laravel框架學(xué)習(xí)筆記之批量更新數(shù)據(jù)功能
這篇文章主要介紹了Laravel框架學(xué)習(xí)筆記之批量更新數(shù)據(jù)功能,結(jié)合實(shí)例形式分析了Laravel框架批量更新數(shù)據(jù)的相關(guān)模型定義與使用操作技巧,需要的朋友可以參考下2019-05-05
Thinkphp5+uploadify實(shí)現(xiàn)的文件上傳功能示例
這篇文章主要介紹了Thinkphp5+uploadify實(shí)現(xiàn)的文件上傳功能,結(jié)合實(shí)例形式分析了Thinkphp5結(jié)合uploadify實(shí)現(xiàn)文件上傳的具體步驟、原理與相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Symfony2針對(duì)輸入時(shí)間進(jìn)行查詢的方法分析
這篇文章主要介紹了Symfony2針對(duì)輸入時(shí)間進(jìn)行查詢的方法,結(jié)合實(shí)例形式分析了Symfony2針對(duì)mysql及MongoDB的輸入時(shí)間進(jìn)行轉(zhuǎn)換與查詢的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
yii2中結(jié)合gridview如何使用modal彈窗實(shí)例代碼詳解
這篇文章主要介紹了yii2中如何結(jié)合gridview使用modal彈窗的相關(guān)資料,需要的朋友可以參考下2016-06-06
Laravel 已登陸用戶再次查看登陸頁(yè)面的自動(dòng)跳轉(zhuǎn)設(shè)置方法
今天小編就為大家分享一篇Laravel 已登陸用戶再次查看登陸頁(yè)面的自動(dòng)跳轉(zhuǎn)設(shè)置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2019-09-09

