zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    ORACLE DROP TABLE和truncate table的区别
    C#版链表加强版
    C#版栈
    再谈为什么要使用MONO
    流浪猫伏击大白鹅
    编写ASP.NET复合控件实例
    C# 大文件拷贝
    关于团队项目构架设计的疑问
    在Windows平台下使用MONO
    C#版链表
  • 原文地址:https://www.cnblogs.com/shiliuye/p/15045059.html
Copyright © 2011-2022 走看看