TP5框架實現(xiàn)簽到功能的方法分析
本文實例講述了TP5框架實現(xiàn)簽到功能的方法。分享給大家供大家參考,具體如下:
基于tp5 模型的一個簽到功能;
由于存儲所有的簽到日期數(shù)據(jù)庫會非常龐大,所以簽到日期只存儲近三個月的。
具體功能:
1、記錄最近一次的簽到時間
2、每次簽到都會添加15積分
3、有連續(xù)簽到的記錄
CREATE TABLE `sp_sign` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主鍵', `times` datetime DEFAULT NULL COMMENT '最近一次簽到時間', `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 '當月簽到給的積分', `one` varchar(255) DEFAULT NULL COMMENT '當月簽到的日期,用“,”隔開', `two` varchar(255) DEFAULT NULL COMMENT '上個月簽到的日期,用“,”隔開', `three` varchar(255) DEFAULT NULL COMMENT '上上個月簽到的日期,用“,”隔開', 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)) //上次簽到時間大于24小時,連續(xù)簽到天數(shù)清零 { $query = Db::name('sign')->where('userid',$userid)->update(['days'=>1]); } else //上次簽到時間小于24小時,連續(xù)簽到次數(shù)加1 { $query = Db::name('sign')->where('userid',$userid)->setInc('days'); } //更新上次簽到時間和簽到積分 $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()); //當前月份 //記錄本次簽到日期 if($sqldate != $nowdate) //上次簽到日期與本次簽到日期月份不一樣 { $oldtime = $times[0]['times']; $onetime=date("Y-m-d H:i:s", strtotime("-1 month")); //獲取前1個月的時間,獲取格式為2016-12-30 13:26:13 $twotime=date("Y-m-d H:i:s", strtotime("-2 month")); //獲取前2個月的時間 $threetime=date("Y-m-d H:i:s", strtotime("-3 month")); //獲取前3個月的時間 $rs = Db::name('sign')->where('userid',$userid)->field('one,two,three')->select(); if($oldtime < $onetime && $oldtime >= $twotime) //月份間隔 大于1個月,小于2個月 { $one = date('d',time()); $two = $rs[0]['one']; $three = $rs[0]['two']; } elseif($oldtime < $twotime && $oldtime >= $threetime) //月份間隔 大于2個月,小于3個月 { $one = date('d',time()); $two = ''; $three = $rs[0]['one']; } elseif($oldtime < $threetime) //月份間隔 大于3個月 { $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; } } }
更多關于thinkPHP相關內容感興趣的讀者可查看本站專題:《ThinkPHP入門教程》、《thinkPHP模板操作技巧總結》、《ThinkPHP常用方法總結》、《codeigniter入門教程》、《CI(CodeIgniter)框架進階教程》、《Zend FrameWork框架入門教程》及《PHP模板技術總結》。
希望本文所述對大家基于ThinkPHP框架的PHP程序設計有所幫助。
- thinkPHP實現(xiàn)簽到功能的方法
- tp5(thinkPHP5)框架數(shù)據(jù)庫Db增刪改查常見操作總結
- tp5(thinkPHP5)框架實現(xiàn)多數(shù)據(jù)庫查詢的方法
- thinkPHP5實現(xiàn)數(shù)據(jù)庫添加內容的方法
- tp5(thinkPHP5)框架連接數(shù)據(jù)庫的方法示例
- thinkPHP5框架數(shù)據(jù)庫連貫操作之cache()用法分析
- thinkPHP5框架實現(xiàn)多數(shù)據(jù)庫連接,跨數(shù)據(jù)連接查詢操作示例
- Thinkphp5框架實現(xiàn)獲取數(shù)據(jù)庫數(shù)據(jù)到視圖的方法
- ThinkPHP5.1框架數(shù)據(jù)庫鏈接和增刪改查操作示例
- PHP利用pdo_odbc實現(xiàn)連接數(shù)據(jù)庫示例【基于ThinkPHP5.1搭建的項目】
- 基于ThinkPHP5框架使用QueryList爬取并存入mysql數(shù)據(jù)庫操作示例
- ThinkPHP5.0框架實現(xiàn)切換數(shù)據(jù)庫的方法分析
相關文章
Laravel框架學習筆記之批量更新數(shù)據(jù)功能
這篇文章主要介紹了Laravel框架學習筆記之批量更新數(shù)據(jù)功能,結合實例形式分析了Laravel框架批量更新數(shù)據(jù)的相關模型定義與使用操作技巧,需要的朋友可以參考下2019-05-05Thinkphp5+uploadify實現(xiàn)的文件上傳功能示例
這篇文章主要介紹了Thinkphp5+uploadify實現(xiàn)的文件上傳功能,結合實例形式分析了Thinkphp5結合uploadify實現(xiàn)文件上傳的具體步驟、原理與相關操作技巧,需要的朋友可以參考下2018-05-05yii2中結合gridview如何使用modal彈窗實例代碼詳解
這篇文章主要介紹了yii2中如何結合gridview使用modal彈窗的相關資料,需要的朋友可以參考下2016-06-06Laravel 已登陸用戶再次查看登陸頁面的自動跳轉設置方法
今天小編就為大家分享一篇Laravel 已登陸用戶再次查看登陸頁面的自動跳轉設置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-09-09