update column if date is older than 30 days - php

I have a table where I have column name 'date' and another column with the name 'status' if I use a cron job to run everyday and check if the date is older than 30 day then to change the status to expired, what code do I need to run using mysqli and php.

MySQL event scheduler is more better.
Start MySQL event scheduler by executing following query in PhpMyAdmin or MySQL command prompt.
Enter this.
SET GLOBAL event_scheduler = 1;
This will update your table.
CREATE EVENT newEvent
ON SCHEDULE EVERY 1 DAY
DO
UPDATE table SET status="expired" WHERE datefield<=CURRENT_DATE - INTERVAL 30 DAY;

Try this:
UPDATE table SET status="expired" where YOURDATEFIELD < date_sub(now(), interval 1 month);

Use INTERVAL in your UPDATE query..
UPDATE table SET status="expired" WHERE datefield<=CURRENT_DATE - INTERVAL 30 DAY

Related

Auto Trigger a Mysql query every day at specific time

I want to give my users bonus every day at 00:01 AM, so i am looking for an automatic trigger in mysql to update Bonus in users.
UPDATE users SET bonus = bonus + 10
How can i do this ?
Thank you
MySQL has a built-in scheduler that can be used to execute tasks according to a given schedule.
Here is how to create a scheduled event for your use case:
create event grant_user_bonus_daily
on schedule every 1 day
starts current_date + interval 1 day + interval 1 minute -- starts tomorrow at 00:01
do
update users set bonus = bonus + 10;
SET GLOBAL event_scheduler = ON;
Example:
CREATE EVENT test_event_02
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MINUTE
ON COMPLETION PRESERVE
DO
INSERT INTO messages(message,created_at)
VALUES('Test MySQL Event 2',NOW());
Full tutorial here
Official MySQL link

PHP - How do i delete data from database automatically after 24 hour of insertion

I want to delete data from database after every 24hours insertion.
For example: A user is added today and he is not active, after 24hours the user will be automatically deleted.
I intend doing this without cron because cron is not in my local/offline server.
Please i really need your help.
You could simply use
DELETE FROM your_table
WHERE your_date_col < DATE_SUB(NOW(), INTERVAL 1 DAY))
and use the mysql scheduled events
could be somethings like this
CREATE EVENT my_dayly_event
ON SCHEDULE
EVERY 1 DAY
COMMENT 'delete old values.'
DO
DELETE FROM your_table
WHERE your_date_col < DATE_SUB(NOW(), INTERVAL 1 DAY))
anyway take a look a this doc. https://dev.mysql.com/doc/refman/5.7/en/create-event.html ... could be useful

Insert data on 1st day of every month only once

I want to insert data into a table on 1st day of every month. And it has to be just for one time. I mean one row for each 1st day of month. I am doing all this in a PHP file and using mySql.
So far i got this much-
if(Date('j')==1)
{
$query = select 1 from table where extract (year from t1) = extract(year from now()) and extract(month from t1) = extract(month from now()) LIMIT 1
if (#mysql_num_rows(mysql_query($query)) == 0)
{
//perform insert operation
}
}
I haven't tried this query till yet.
Maybe this will do the trick
SELECT * FROM xxx WHERE (DATE_FORMAT(CURDATE() ,'%d')==1)
SELECT DATEADD(mm, DATEDIFF(mm,0,GETDATE())-1,0) as StartOfLastMonth
SELECT DATEADD(mm, DATEDIFF(mm,0,GETDATE()),0) as StartOfThisMonth
SELECT DATEADD(mm, DATEDIFF(mm,0,GETDATE()),0) as StartOfNextMonth
Try out below event scheduler
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 MONTH
DO
"YOUR QUERY"
Your have to run this on the date on which you want to run on 1 month interval.
You get more information on http://dev.mysql.com/doc/refman/5.1/ja/create-event.html
You have two solutions, depending on what you exactly want.
If your data must be inserted at the first day: the only way is to insert it using a cron job which run at the correct time. You can use the mysql scheduler to do the job if it's only mysql related.
If your data need to be in the table after the first day, but you don't care if it is added in realtime or only when you access that data then you don't need a cron job. You can check for that data when you access it, then insert it before reading if it's not already present.
you can achieve with mysql event scheduler--
http://dev.mysql.com/doc/refman/5.1/en/events.html
CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
COMMENT 'Clean up Service Start at 11:00PM daily!'
DO DELETE FROM my_table WHERE created_date < (NOW() - INTERVAL 1 MONTH);
OR for Stored Procedure.
CREATE EVENT IF NOT EXISTS `my_old_data_cleaner_event`
ON SCHEDULE EVERY 23 DAY_HOUR
DO CALL my_sp_cleanup_old_data();

Deleteing record from MySQL Database after 1 Week

I want to know how to delete records from a MySQL Database after 1 week
This is the code I have for inserting the record and date of when it was added
INSERT INTO moviesdone VALUES ('" . $id2 . "', NOW())
Where $id is the name of what I am insterting and wanting to delete after 1 week.
I would also like to do this automatically, which I can set up from in phpMyAdmin
So, I only need to know: How do I write the date correctly (if needed) and how do I delete the record one week later?
Run this code at regular intervals, for example once per day:
DELETE FROM moviesdone
WHERE datetimecol < NOW() - INTERVAL 7 DAY
You can use the event scheduler.
If you are using MySQL 5.1.6 or greater you can use CREATE EVENT
Something like
CREATE EVENT del_entries
ON SCHEDULE EVERY 1 DAY
DO
DELETE FROM table WHERE DATEDIFF(NOW(), creation_date) >= 7;

How to auto update a record in database?

I have a jobs table in which I inserts new jobs.
This table also contains the job post date.
By default the job status is open when a new insertion take place.
Now I want to change the status of the job from open to close when the jobs becomes older than 30 days.
How will I do this?
Try creating a event which runs every day like below
CREATE EVENT myevent
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY
DO
UPDATE my_table SET status ='closed'
WHERE post_date > DATE_ADD(now(), INTERVAL -30 DAY)
AND status='open'
-- Update Changed syntax
CREATE EVENT myevent
ON SCHEDULE EVERY 24 HOUR
DO
UPDATE my_table SET status ='closed'
WHERE post_date > DATE_ADD(now(), INTERVAL -30 DAY)
AND status='open'
Use a cron job to handle this. http://en.wikipedia.org/wiki/Cron
If you can't handle crons, you could do a "poor man's cron". Means the actual process that updates all of your jobs records takes place when someone visits your page (with additional checks when the last run was). If you're doing it the "poor" way I suggest to fire off another thread to keep your site responsible.

Categories