Home PHP MySQL query shows the current date of the table by date before 1 month
Reply: 1

PHP MySQL query shows the current date of the table by date before 1 month

Andreas Nex
1#
Andreas Nex Published in 2018-01-13 07:39:16Z

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.

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 :

+------+------------+------------+------------+
| 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 :

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.

scaisEdge
2#
scaisEdge Reply to 2018-01-13 07:46:31Z

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

  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:

  date(DATE_SUB(deadline, INTERVAL 1 MONTH))
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.35014 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO