dongshen2903 2015-10-27 10:54
浏览 24

如何为日期类型数据设置条件

I have table for tasks list.

how can I create condition for dates?

I want to give red color for tasks that their targerDate are in 2 days or less.

    $mainQuery = mysql_query("SELECT * FROM `tasks `");
    while($mainIndex = mysql_fetch_array($mainQuery)) 
{
if ($mainIndex['tagertDate'] <= ???)
}

sql table:

id  int(10) 
title   varchar(250)    utf8_general_ci
targetDate  date    
  • 写回答

1条回答 默认 最新

  • dongyi7513 2015-10-27 11:13
    关注

    You can Use PHP's DateTime and Diff function to get the desired result.

    Following Function return differnce between current date and provided date in days.

    function getDays($date) {
        $datetime1 = new DateTime($date);
        $datetime2 = new DateTime();
        $noOfDays= $datetime1->diff($datetime2);
        return $noOfDays->format('%R%a days');
    }
    

    Update:Use a function to compare date like this

    Here is check that Say if days returned by getDays($mainIndex['tagertDate']) is less than or eqaul to 2.

    if(getDays($mainIndex['tagertDate']) <= 2) {
        Make things colored here
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向