du13520157325 2016-10-01 00:18
浏览 45
已采纳

PHP在日期和今天日期之间计数

In my dates database I have a table of dates that have two followups to be completed, one after 30 days, one after 60 days. I need to build a page that uses the MySQL query to pull all dates from the dates table that have a 30day value of No (which I can do). Now the tricky part is, I need it to only output the dates that meet that criteria, and are 30 days from the current date.

For example: August 4 & 6 have a 30day value of No, August 5 has a 30day value of Yes. Today's date is September 4. 30-days prior would be August 5.

I need the query to only display August 4 in this case, since it hasn't been 30 days since August 6 and August 5 has already been done.

I am unsure what kind of function to use to do this counting. I appreciate your help

EDIT:

Date - 30day Value
July 1 - Yes
July 5 - No
August 1 - No
August 5 - No
August 6 - Yes

Today's Date is September 2.

The table would display July 5 and August 1, as their 30day values are No, and they are more than 30 days from todays date.

  • 写回答

3条回答 默认 最新

  • douchengchen7959 2016-10-01 18:40
    关注

    MySQL's DATEDIFF function allows you to subtract 2 dates in a query.

    http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_datediff

    DATEDIFF() returns expr1 − expr2 expressed as a value in days from one date to the other. expr1 and expr2 are date or date-and-time expressions. Only the date parts of the values are used in the calculation.

    For example:

    SELECT some_id, date_column
    FROM date_table
    WHERE DATEDIFF(CURDATE(), date_column) = 30
    

    You could also select both 30 and 60 days like this and also have a cutoff date of 60 days so it's not searching the whole table:

    SELECT some_id, date_column
    FROM date_table
    WHERE date_column>=DATE_SUB(CURDATE(), INTERVAL 60 DAY)
    AND DATEDIFF(CURDATE(), date_column) IN (30, 60)
    

    And since I'm making some assumptions with my understanding of what you're asking, you may also want to do this which will return the results as 'Yes' or 'No' in your result set:

    SELECT some_id, date_column,
    CASE DATEDIFF(CURDATE(), date_column)
        WHEN 60 THEN 'Yes'
        WHEN 30 THEN 'Yes'
        ELSE 'No'
    END CASE AS is_3060_day
    FROM date_table
    WHERE date_column>=DATE_SUB(CURDATE(), INTERVAL 60 DAY)
    

    Alternatively if you want to accomplish this on the PHP side, you could use PHP's date_diff function:

    http://php.net/manual/en/function.date-diff.php

    function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
    {
        $datetime1 = date_create($date_1);
        $datetime2 = date_create($date_2);
    
        $interval = date_diff($datetime1, $datetime2);
    
        return $interval->format($differenceFormat);
    
    }
    $result = dateDifference($date1, $date2)
    if ($result==30 || $result==60) {
        // Do something
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器