TP5框架實(shí)現(xiàn)簽到功能的方法分析
本文實(shí)例講述了TP5框架實(shí)現(xiàn)簽到功能的方法。分享給大家供大家參考,具體如下:
基于tp5 模型的一個(gè)簽到功能;
由于存儲(chǔ)所有的簽到日期數(shù)據(jù)庫會(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ù)庫Db增刪改查常見操作總結(jié)
- tp5(thinkPHP5)框架實(shí)現(xiàn)多數(shù)據(jù)庫查詢的方法
- thinkPHP5實(shí)現(xiàn)數(shù)據(jù)庫添加內(nèi)容的方法
- tp5(thinkPHP5)框架連接數(shù)據(jù)庫的方法示例
- thinkPHP5框架數(shù)據(jù)庫連貫操作之cache()用法分析
- thinkPHP5框架實(shí)現(xiàn)多數(shù)據(jù)庫連接,跨數(shù)據(jù)連接查詢操作示例
- Thinkphp5框架實(shí)現(xiàn)獲取數(shù)據(jù)庫數(shù)據(jù)到視圖的方法
- ThinkPHP5.1框架數(shù)據(jù)庫鏈接和增刪改查操作示例
- PHP利用pdo_odbc實(shí)現(xiàn)連接數(shù)據(jù)庫示例【基于ThinkPHP5.1搭建的項(xiàng)目】
- 基于ThinkPHP5框架使用QueryList爬取并存入mysql數(shù)據(jù)庫操作示例
- ThinkPHP5.0框架實(shí)現(xiàn)切換數(shù)據(jù)庫的方法分析
相關(guān)文章
php計(jì)算程序運(yùn)行時(shí)間的簡(jiǎn)單例子分享
這篇文章主要介紹了php計(jì)算程序運(yùn)行時(shí)間的簡(jiǎn)單例子分享,需要的朋友可以參考下2014-05-05
php array_walk_recursive 使用自定的函數(shù)處理數(shù)組中的每一個(gè)元素
php中,如果需要對(duì)數(shù)組中的每個(gè)元素進(jìn)行一定規(guī)則的處理,我們可以使用array_walk_recursive,array_walk_recursive函數(shù)用于對(duì)數(shù)組中的每個(gè)成員遞歸地應(yīng)用用戶函數(shù)。本文章通過實(shí)例向大家講解array_walk_recursive函數(shù)的使用方法2016-11-11
CI框架學(xué)習(xí)筆記(一) - 環(huán)境安裝、基本術(shù)語和框架流程
本文是CI框架學(xué)習(xí)筆記的第一篇,主要介紹了CI框架的環(huán)境安裝,基本術(shù)語以及框架流程,非常的詳細(xì),有需要的朋友可以參考下2014-10-10
php curl獲取到j(luò)son對(duì)象并轉(zhuǎn)成數(shù)組array的方法
今天小編就為大家分享一篇php curl獲取到j(luò)son對(duì)象并轉(zhuǎn)成數(shù)組array的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05

