多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# ThinkPHP6 杂项 --- ## `ThinkPHP6` 细节 ### 1、创建公用方法 ```php app/common.php 示例: /** * 取某天零点时间戳(本地时间) * @param number $time 欲获取当日的时间戳 * return number 这天零点时间戳 */ function getZeroTime($time){ // Z 时差偏移量的秒数。UTC 西边的时区偏移量总是负的,UTC 东边的时区偏移量总是正的。 $timezone = date('Z', $time); $time += $timezone; $time = $time - $time % 86400 - $timezone; return $time; } ``` ```php controller示例: namespace app\index\controller; use app\BaseController; class Index extends BaseController{ public function index(){ $time = getZeroTime('1561785828'); print_r($time.'<br/>'); print_r(date('Y-m-d H:i:s',$time)); } } ``` ### 2、创建共用类方法 ```php BaseController 示例: /** * 取某天零点时间戳(本地时间) * @param number $time 欲获取当日的时间戳 * return number 这天零点时间戳 */ public function getZeroTime($time){ // Z 时差偏移量的秒数。UTC 西边的时区偏移量总是负的,UTC 东边的时区偏移量总是正的。 $timezone = date('Z', $time); $time += $timezone; $time = $time - $time % 86400 - $timezone; return $time; } ``` ```php controller示例: namespace app\index\controller; use app\BaseController; class Index extends BaseController{ public function index(){ $time = $this->getZeroTime('1561785828'); print_r($time.'<br/>'); print_r(date('Y-m-d H:i:s',$time)); } } ``` ### 3、事务操作 * 使用事务处理的话,需要数据库引擎支持事务处理。比如 `MySQL` 的 `MyISAM` 不支持事务处理,需要使用 `InnoDB` 引擎。 ```php namespace app\index\controller; use app\BaseController; use think\facade\Db; class Index extends BaseController{ public function index(){ Db::startTrans(); $delete = Db::table('admin')->delete(1); if($delete){ Db::commit(); }else{ Db::rollback(); } } } ```