Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql auto update event

using php and MySql, is there anyway to get the date in the database to update by its self when a date has expired. i.e. event name x is on date 2012-05-12, on 2012-05-13 the date should change to 2012-05-19 (a week from 2012-05-12)

thanks guys

like image 896
Saeid Ghaferi Avatar asked May 20 '26 05:05

Saeid Ghaferi


1 Answers

You can use MySQL's event scheduler:

CREATE EVENT update_date ON SCHEDULE EVERY 1 DAY STARTS CURDATE() DO
  UPDATE events_table
  SET    event_date = ADDDATE(event_date, INTERVAL 1 WEEK)
  WHERE  event_date < CURDATE();

In order to make the event scheduler run you can add this in my.cnf:

[mysqld]
...
event_scheduler=ON

and with superuser privileges, you can set the global variable on the fly:

SET GLOBAL event_scheduler='ON';
like image 188
eggyal Avatar answered May 21 '26 17:05

eggyal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!