讲师中心 微信公众号
AI工具推荐 视频效率加速

ThinkPHP5数据库实例详解

浏览21089
更新时间2022-01-20

whereTime方法

1、功能:专用于日期时间型字段的查询

whereTime可完全用where方法代替

2、源码:/thinkphp/libray/think/db/Query.php

    /**
     * 查询日期或者时间
     * @access public
     * @param string       $field 日期字段名
     * @param string       $op    比较运算符或者表达式
     * @param string|array $range 比较范围
     * @return $this
     */
    public function whereTime($field, $op, $range = null)
    {
        if (is_null($range)) {
            // 使用日期表达式
            $date = getdate();
            switch (strtolower($op)) {
                case 'today':
                case 'd':
                    $range = 'today';
                    break;
                case 'week':
                case 'w':
                    $range = 'this week 00:00:00';
                    break;
                case 'month':
                case 'm':
                    $range = mktime(0, 0, 0, $date['mon'], 1, $date['year']);
                    break;
                case 'year':
                case 'y':
                    $range = mktime(0, 0, 0, 1, 1, $date['year']);
                    break;
                case 'yesterday':
                    $range = ['yesterday', 'today'];
                    break;
                case 'last week':
                    $range = ['last week 00:00:00', 'this week 00:00:00'];
                    break;
                case 'last month':
                    $range = [date('y-m-01', strtotime('-1 month')), mktime(0, 0, 0, $date['mon'], 1, $date['year'])];
                    break;
                case 'last year':
                    $range = [mktime(0, 0, 0, 1, 1, $date['year'] - 1), mktime(0, 0, 0, 1, 1, $date['year'])];
                    break;
            }
            $op = is_array($range) ? 'between' : '>';
        }
        $this->where($field, strtolower($op) . ' time', $range);
        return $this;
    }

源码分析:

  1. 首先获取到当前时间 :getDate(),获取到一个时间数组:
array(11) {
  ["seconds"] => int(46)
  ["minutes"] => int(59)
  ["hours"] => int(16)
  ["mday"] => int(13)
  ["wday"] => int(0)
  ["mon"] => int(11)
  ["year"] => int(2016)
  ["yday"] => int(317)
  ["weekday"] => string(6) "Sunday"
  ["month"] => string(8) "November"
  [0] => int(1479027586)
}
  1. 此时,将该方法的第三个参数:range视为时间表达式,根据它的值进行判断,从$date数组中取中对应的数据:

    • 例如:$op='year' 或 'y',表示‘年’,则取出date['year'],赋值第三个参数:$range;
  2. mktime(小时,分钟,秒, 月,日,年),返回时间戳,strtotime()将日期型字符串转时间戳;

  3. 如果$range是数组,则把操作符$op变更成:between,进行区间查询

  4. 最后仍是调用 where 方法完成查询

3、基本语法:

与上节介绍的where非常相似,可视为where中专门针对日期字段查询的特例。语法上看,可以简单记为:查询表达式中的time,做为方法名单词第二部分即可。即将where中查询表达式中的time,移到方法名where后面做后缀,即whereTime 。

Db::table('表名') -> field('字段列表') ->whereTime('日期类型字段','条件表达式', '日期格式') ->select();

4、实例演示:

我们仍以上节课的例子,演示比较查询和区间查询

1.任务1:查询tp5_staff表中2015年1月1日以后入职的员工信息

  • Index.php 控制器代码:
<?php
namespace app\index\controller;
//导入数据库类
use think\Db;

class Index {
  public function index(){   
   $result = Db::table('tp5_staff')
          -> field('id,name,hiredate')
          ->whereTime('hiredate','>', '2015-01-01')
          ->select();

  dump($result);
  }
}
  • 查询结果:
array(5) {
  [0] => array(3) {
    ["id"] => int(1006)
    ["name"] => string(9) "西门庆"
    ["hiredate"] => string(10) "2015-12-25"
  }
  [1] => array(3) {
    ["id"] => int(1007)
    ["name"] => string(9) "潘金莲"
    ["hiredate"] => string(10) "2016-03-14"
  }
  [2] => array(3) {
    ["id"] => int(1008)
    ["name"] => string(6) "宋江"
    ["hiredate"] => string(10) "2015-12-31"
  }
  [3] => array(3) {
    ["id"] => int(1023)
    ["name"] => string(9) "段王爷"
    ["hiredate"] => string(10) "2015-12-31"
  }
  [4] => array(3) {
    ["id"] => int(1028)
    ["name"] => string(6) "方方"
    ["hiredate"] => string(10) "2015-12-31"
  }
}
  • 对应的SQL语句:
SELECT `id`,`name`,`hiredate` FROM `tp5_staff` WHERE `hiredate` > '2015-01-01'

2. 任务2:查询tp5_staff表中2015年1月1日到2016年1月1日之间入职的员工

  • Index.php 控制器源码
<?php
namespace app\index\controller;
//导入数据库类
use think\Db;

class Index {
  public function index(){   
   $result = Db::table('tp5_staff')
          -> field('id,name,hiredate')
          ->whereTime('hiredate','between', ['2015-01-01','2016-01-01'])
          ->select();

  dump($result);
  }
}
  • 查询结果:
array(4) {
  [0] => array(3) {
    ["id"] => int(1006)
    ["name"] => string(9) "西门庆"
    ["hiredate"] => string(10) "2015-12-25"
  }
  [1] => array(3) {
    ["id"] => int(1008)
    ["name"] => string(6) "宋江"
    ["hiredate"] => string(10) "2015-12-31"
  }
  [2] => array(3) {
    ["id"] => int(1023)
    ["name"] => string(9) "段王爷"
    ["hiredate"] => string(10) "2015-12-31"
  }
  [3] => array(3) {
    ["id"] => int(1028)
    ["name"] => string(6) "方方"
    ["hiredate"] => string(10) "2015-12-31"
  }
}
  • 对应的SQL语句:
SELECT `id`,`name`,`hiredate` FROM `tp5_staff` WHERE `hiredate` BETWEEN '2015-01-01' AND '2016-01-01'
  • SQLPRO for MySQL 中查询结果:

5、总结:

whereTime 与 where 无本质区别,可视为where针对日期字段的快捷方式,实际开发,具体用哪个随个人喜好,既然用了TP5,就尽可能选择使用TP5的新特性。

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn