<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                需求:開發中有時會遇到工作日的問題,比如一個審批需要三個工作日,我們在錄入的時候就需要知道三個工作日之后的日期。 原理:**利用mysql存儲未來指定天數的工作日時間戳,然后利用limit查找** ##### 1.調用方法: ~~~php $getTimeClass = new app\common\common\GetTime(); return $getTimeClass->get_after_date(5); //獲取5個工作日之后的日期 ~~~ ##### 2.類demo: ~~~php <?php namespace app\common\common; use think\facade\{Db,Config}; class GetTime { /** * [天行數據] * [接口文檔url:https://www.tianapi.com/apiview/139] * @var [type] */ private $readme; /** * [天行數據接口地址] * @var string */ private $base_url = "http://api.tianapi.com/txapi/jiejiari/index"; /** * [天行數據接口秘鑰] * @var string */ private $key = "d5ebdaf781005a07a5ca77d62531bb0e"; /** * [數據表前綴] * @var [type] */ protected $tablePrefix; /** * [數據表名字] * @var [type] */ protected $tableName; /** * [拉取天數] * @var integer */ private $time = 120; /** * [構造函數初始化表前綴和表名稱] * @Author juming * @DateTime 2021-01-26T16:18:07+0800 */ public function __construct(){ $this->tablePrefix = Config::get('database.connections.mysql.prefix'); $this->tableName = "{$this->tablePrefix}workdays"; return $this; } /** * [獲取指定個工作日的日期] * @Author juming * @DateTime 2021-01-26T16:19:05+0800 * @param int $after [description] * @return [type] [description] */ public function get_after_date(int $after){ if($after > 30){ throw new \Exception("Only query data within 30 days is supported"); } self::check_table_exists(); $timestamp = self::get_before_dawn_time(); $data = Db::table($this->tableName)->where('timestamp','>',$timestamp)->limit($after)->column('timestamp'); if(count($data) != $after){ return self::request_and_get_data($after); } $end = end($data); return date('Y-m-d',$end); } /** * [請求天行接口拉取數據] * @Author juming * @DateTime 2021-01-26T16:20:39+0800 * @param int $after [description] * @return [type] [description] */ protected function request_and_get_data(int $after){ set_time_limit(0); $timestamp = self::get_last_date(); for($i=1;$i<=$this->time;$i++){ $date = date('Y-m-d',$timestamp + $i*86400); $get_url = self::get_url($date); $res = json_decode(file_get_contents($get_url),true); if($res['code'] != 200){ throw new \Exception("接口調用失敗(錯誤碼:".$res['code'].")"); } if($res['newslist'][0]['isnotwork'] == 0){ $insert['timestamp'] = strtotime($date); Db::table($this->tableName)->insert($insert); } } return self::get_after_date($after); } /** * [獲取接口的url] * @Author juming * @DateTime 2021-01-26T16:21:41+0800 * @param String $date [description] * @return [type] [description] */ private function get_url(String $date){ return $this->base_url.'?key='.$this->key.'&date='.$date; } /** * [獲取數據表中最后的數據日期] * @Author juming * @DateTime 2021-01-26T16:22:47+0800 * @return [type] [description] */ private function get_last_date(){ $timestamp = Db::table($this->tableName)->order('id','desc')->limit(1)->value('timestamp'); $timestamp = $timestamp ? $timestamp :$this->get_before_dawn_time();; return $timestamp; } /** * [獲取當日凌晨時間戳] * @Author juming * @DateTime 2021-01-26T16:23:12+0800 * @return [type] [description] */ protected function get_before_dawn_time(){ return strtotime(date('Y-m-d')); } /** * [檢查表是否存在] * @Author juming * @DateTime 2021-01-26T16:23:34+0800 * @return [type] [description] */ protected function check_table_exists(){ $check = Db::query("show tables like '{$this->tableName}'"); if (empty($check)) { $sql = self::getCreateSql(); Db::execute($sql); } } /** * [創建數據表sql語句] * @Author juming * @DateTime 2021-01-26T16:23:58+0800 * @return [type] [description] */ protected function getCreateSql(){ return <<<EOT CREATE TABLE `{$this->tableName}` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '自增主鍵', `timestamp` int(11) DEFAULT '0' COMMENT '當日時間戳', PRIMARY KEY (`id`), KEY `timestamp` (`timestamp`) USING BTREE COMMENT '當日凌晨時間戳索引' ) ENGINE=MyISAM AUTO_INCREMENT=61 DEFAULT CHARSET=utf8 COMMENT='工作日記錄表'; EOT; } } ~~~ ##### 3.說明: 調用類方法時,會首先檢查數據庫中workdays表是否存在,如果不存在會自動創建workdays表,無需手動創建,使用前請確保數據庫配置正確。 作者:JuMinggniMuJ 來源:簡書 著作權歸作者所有。商業轉載請聯系作者獲得授權,非商業轉載請注明出處。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看