PHP MySQL查询在1个月之前按日期显示表的当前日期

时间:2022-10-28 22:37:08

I will create a notification system with 1 month notice before deadline. I have a problem displaying a date that coincides with the current date. I want to show that appears only date today from the deadline date before 1 month deadline.

我将在截止日期前1个月通知创建一个通知系统。我在显示与当前日期一致的日期时遇到问题。我想表明,从截止日期截止日期前的截止日期开始,今天只显示日期。

My tabel = notif :

我的表格=通知:

+------+------------+------------+
| id   | name       | deadline   |
+------+------------+------------+
| 01   | Daniel     | 2018-02-13 |
| 02   | Elwin      | 2018-02-12 |
| 03   | Dika       | 2018-02-13 |
+------+------------+------------+

For example : date today = 2018-01-13
report table what i want :

例如:date today = 2018-01-13 report table我想要的:

+------+------------+------------+------------+
| id   | name       | deadline   | alert_now  |
+------+------------+------------+------------+
| 01   | Daniel     | 2018-02-13 | 2018-01-13 |
| 03   | Dika       | 2018-02-13 | 2018-01-13 |
+------+------------+------------+------------+

My Query in model of codeigniter :

我在codeigniter模型中的查询:

public function notification(){
        $query=$this->db->query('SELECT *, DATE_SUB(deadline, INTERVAL 1 MONTH) as alert_now FROM notif WHERE now() = DATE_SUB(deadline, INTERVAL 1 MONTH)');
        return $result=$query;
    }

Note : Sorry, my english is poor. Thank you.

注意:对不起,我的英语很差。谢谢。

1 个解决方案

#1


0  

Assuming you deadline is date type Instead of now() (date time ) you should use curdate() (date)

假设你的截止日期是日期类型而不是now()(日期时间)你应该使用curdate()(日期)

  public function notification(){
          $query=$this->db->query('SELECT *, DATE_SUB(deadline, INTERVAL 1 MONTH) as alert_now 
                    FROM notif WHERE curdate() = DATE_SUB(deadline, INTERVAL 1 MONTH)');
          return $result=$query;
      }

if deadline is datetime then you should get the date parte eg:

如果截止日期是datetime那么你应该得到日期parte,例如:

  date(DATE_SUB(deadline, INTERVAL 1 MONTH))

#1


0  

Assuming you deadline is date type Instead of now() (date time ) you should use curdate() (date)

假设你的截止日期是日期类型而不是now()(日期时间)你应该使用curdate()(日期)

  public function notification(){
          $query=$this->db->query('SELECT *, DATE_SUB(deadline, INTERVAL 1 MONTH) as alert_now 
                    FROM notif WHERE curdate() = DATE_SUB(deadline, INTERVAL 1 MONTH)');
          return $result=$query;
      }

if deadline is datetime then you should get the date parte eg:

如果截止日期是datetime那么你应该得到日期parte,例如:

  date(DATE_SUB(deadline, INTERVAL 1 MONTH))