• thinkphp5日期时间查询比较和whereTime使用方法


    一、使用where方法进行时间的比较查询

    where(‘create_time’,’> time’,’2019-1-1′); // 大于某个时间
    where(‘create_time’,'<= time’,’2019-1-1′); // 小于某个时间
    where(‘create_time’,’between time’,[‘2018-1-1′,’2019-1-1’]); // 时间区间查询

    二、使用whereTime方法

    whereTime(‘birthday’, ‘>=’, ‘1970-10-1’)->select(); // 大于某个时间
    
    whereTime(‘birthday’, ‘<‘, ‘2000-10-1’)->select(); // 小于某个时间
    
    whereTime(‘birthday’, ‘between’, [‘1970-10-1’, ‘2000-10-1’])->select(); // 时间区间查询
    
    whereTime(‘birthday’, ‘not between’, [‘1970-10-1’, ‘2000-10-1’])->select(); // 不在某个时间区间

    三、时间表达式

    // 获取今天的文章
    Db::table(‘think_news’) ->whereTime(‘create_time’, ‘today’)->select();
    // 获取昨天的文章
    Db::table(‘think_news’)->whereTime(‘create_time’, ‘yesterday’)->select();
    // 获取本周的文章
    Db::table(‘think_news’)->whereTime(‘create_time’, ‘week’)->select();
    // 获取上周的文章
    Db::table(‘think_news’)->whereTime(‘create_time’, ‘last week’)->select();
    // 获取本月的文章
    Db::table(‘think_news’)->whereTime(‘create_time’, ‘month’)->select();
    // 获取上月的文章
    Db::table(‘think_news’)->whereTime(‘create_time’, ‘last month’)->select();
    // 获取今年的文章
    Db::table(‘think_news’)->whereTime(‘create_time’, ‘year’)->select();
    // 获取去年的文章
    Db::table(‘think_news’)->whereTime(‘create_time’, ‘last year’)->select();

    四、如果查询当天、本周、本月和今年的时间,还可以简化为:

    // 获取今天的文章
    Db::table(‘think_news’)->whereTime(‘create_time’, ‘d’)->select();
    // 获取本周的文章
    Db::table(‘think_news’)->whereTime(‘create_time’, ‘w’)->select();
    // 获取本月的文章
    Db::table(‘think_news’)->whereTime(‘create_time’, ‘m’)->select();
    // 获取今年的文章
    Db::table(‘think_news’)->whereTime(‘create_time’, ‘y’) ->select();

    五、时间范围查询

    // 查询两个小时内的文章
    Db::table(‘think_news’)->whereTime(‘create_time’,’-2 hours’)->select();

    转自:http://www.weixuecn.cn/article/14054.html

  • 相关阅读:
    基于NFS实现多WEB服务器负载均衡
    CentOS 6编译安装lamp,并分别安装event模块方式和FPM方式的PHP
    CentOS 7 下的LAMP实现以及基于https的虚拟主机
    ssh 免密码设置失败原因总结
    任督二脉之进程管理(3)
    任督二脉之进程管理(4)
    任督二脉之进程管理(1)
    任督二脉之进程管理(2)
    VIRTIO概述和基本原理
    图解 TCMalloc
  • 原文地址:https://www.cnblogs.com/shiliuye/p/15045059.html
Copyright © 2020-2023  润新知