duangongqiong6958 2011-08-25 10:33
浏览 39
已采纳

测距日期中的SQL WHERE子句计算

I Have project in which a module needs to display the list of data using a group of specific criteria:

Today
Week
Month
Year

The Database table contains a date field which has datatype as BIGINT(10) and through PHP code we insert time() function value into it. which insert some value like 1311144077,

Now I need to fetch the records from my table as per the labels mentionedabove, how can i do that?

  • 写回答

3条回答 默认 最新

  • douao1858 2011-08-25 11:04
    关注

    While you're storing datetime in epoch timestamp format, I think solutions using MySQL date/time functions could be very slow. So, I suggest using timestamp itself like below codes.

    Usage example

    $t = time();
    
    # today
    echo date('Y-m-d H:i:s', strtotime('today', $t))."
    ";
    echo date('Y-m-d H:i:s', strtotime('tomorrow', $t))."
    ";
    
    # this week  
    echo date('Y-m-d H:i:s', strtotime('-1 sunday', $t))."
    ";
    echo date('Y-m-d H:i:s', strtotime('sunday', $t))."
    ";
    
    # this month  
    echo date('Y-m-d H:i:s', strtotime(date('Y-m-01 00:00:00', $t)))."
    ";
    echo date('Y-m-d H:i:s', strtotime(date('Y-m-01 00:00:00', strtotime('next month', $t))))."
    ";
    
    # this year
    echo date('Y-m-d H:i:s', strtotime(date('Y-01-01 00:00:00', $t)))."
    ";
    echo date('Y-m-d H:i:s', strtotime(date('Y-01-01 00:00:00', strtotime('next year', $t))))."
    ";
    

    In query

    # this year
    $start_of_year = strtotime(date('Y-01-01 00:00:00', $t));
    $end_of_year = strtotime(date('Y-01-01 00:00:00', strtotime('next year', $t)));
    $query = "SELECT * FROM sometable WHERE somecolumn >= $start_of_year AND somecolumn < $end_of_year";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?