Delete a post after a period of time - php

I have created a function that gets the time an article was published. The same function checks whether the article is 1 week old and deactivates it.
I don't know what to do here if I have to make the function responsive when I call. I thought of using JavaScript and setting a timeout for the function. that would be too much of looping.
This is the "if" that I used to check the date. Someone with a better idea, please help.
if((time() - $publish_time) > (7*24*60*60)){
// if the current timestamp - the time the article was published > 1 week.
//Execute the query to deactivate the article
}
That snippet is inside a function that I have to call.

Imagine that [table] has [column] with the date of the post got inserted:
SET GLOBAL event_scheduler = ON;
CREATE
EVENT 'archive_posts'
ON SCHEDULE EVERY 1 WEEK
DO
DELETE FROM [table] WHERE [column] < DATE_SUB(NOW(), INTERVAL 1 WEEK);

Rather than having this inside your site, you should expire your articles with a cronjob, as per this question when using MySQL (*nix-like)
On Windows, you can use mmc.exe and add a Task to execute the script through Task Scheduler)

Related

Delete database row with specific value after 1 hour php

I'm planning on adding destroyable messages to my website.
I want users to choose how long the message should be 'alive'.
The message should be destroyed a specific time after creation only if the row meets a column value.
The important columns are:
time, destroy
time is stored as DATETIME in the database.
I want a row to be deleted 1 hour after creation only if destroy equals '1'
I'd highly appreciate it if someone could point me in the right direction as I have no clue on how to approach this.
Thanks in advance
I've read about using cron jobs but I failed to do so.
This is what i've tried:
cron.php
<?php
include 'includes/db.php';
mysql_query("DELETE FROM `d_msg_enc` WHERE `mode` = 1 AND `time` > DATE_SUB(NOW() INTERVAL 10 MINUTE)");
?>
You can not command php script to run after 1 hour (or schedule scripts) .. To accomplish this type of scheduling of scripts use CRONS..
Refer this url for more detail: https://stackoverflow.com/a/18737637/4419992

Automatically update data in sql for a webpage

So I'm a software Development student and for my web class I created a project that uses among other things Php and SQL; In this project, users can create posts and other users can comment on them.
The thing is I want posts to only be available for a certain period of time.
Then I have an SQL table named 'Posts' and they have a column named 'Status' (you know, if the status it's 0 they're not available and else they are.)
When a user creates a post I make SQL:
INSERT INTO posts *All the post data*, I set the Status to 1 and make a TIMESTAMP to register the date of creation of the post. I want that a week after the date registered in the Timestamp changes the status column to 0 but I don't want it to be with a page request (I need it to be automatic) and I want the user to be notified via email or something.
Can it be made with some python CGI that checks the date, updates the Status and sends the email or is there a better/easier way to do it?
Thanks a lot for your help :)
You dont need the status 0/1 AND the timestamp column, if all you want to do is show a post for a set period of time.
Just use the timestamp column and amend the queries that fetch the posts to only show those posts that are < 7 days old (or any period you decide)
EG
SELECT * from posts where timestamp_col < DATE_SUB(NOW(), INTERVAL 7 DAY)
or something similiar that meets your needs
Turns out the best way to solve this was using Cron Jobs.
I run a PHP script every day and I modify the posts which are exactly 7 days old, using
UPDATE Posts SET Status = 0 WHERE DATE(timestamp_col) = DATE_SUB(NOW(), INTERVAL 7 DAY)
And then I iterate through the affected rows emailing the users.

how to make data base to delete info after some time using php?

I need to delete particular info from data base using only php, after some time without using cron system. How can I realize it?
Without cron there is only one way i.e hook that deletion code with some event e.g login of any user. As a new user logs in you can run that code
Include a timestamp in the database table. Then have a function in your PHP that deletes all records that are more than X minutes old whenever the PHP is run.
You can use a query such as this (for all records more than a day old).
DELETE FROM `table`
WHERE `timestamp` < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 DAY))

PHP set interval of a process

I have an API fetcher script that reads values from XML files and updates data to mysql. It updates the database everytime it's called via ajax.
Now I want to set an interval for about a day before the query gets updated. Which php function or method should I use?
I think I should make myself a little more clear, these are the steps:
xml file read with php
Echo the results.
--->If (mysql updated time > 24*60*60 seconds) update the db
--->if not, do nothing.
I can't figure out how should I process the step (3)
Well! as far as i understood your question. I think you should do with SQL not with PHP.
Save update time in db with query of Insert. And call a stored procedure every time you update. like in MySQL:
Begin
//Adding updtime column value into a Variable called #a
select #a:=updtime from abc order by id desc limit 1;
//DATE_ADD() is a function of MySQL to get Time after adding Hours or Minutes etc. More can be found in MySQL Manual online
if DATE_ADD(#a, INTERVAL 24 HOUR) > now()
then insert into abc values ('XML',now());
end if;
End
You can also do it without a variable and that would be much better as
if DATE_ADD((select updtime from abc order by id desc limit 1), INTERVAL 24 HOUR) > now()
The best solution that I can think of is a Cron Job.
This page will give you detailed information

Remove Mysql row after specified time

I'm trying to create a computer reservation system, where user chooses a computer and select the time how long he will be using this PC. In that time other persons can't reserve this pc, I need to find a solution, how to automaticaly delete all rows containing reserved pc's after their time expires. Thank you for the advice.
The common way to handle this is to store an expires_at timestamp on the reservation row. Then your query to find any "open" reservations would have WHERE 'expires_at' < NOW() or something similar.
This is an untested answer, that may only be a suggestion, but I just started looking at these, so am interested in feedback as well. i'm still working through possibilities and drawbacks, but it might well suit your need.
Take a look at MySQL Events, an article about it is here, and official syntax at Mysql Docs.
Per the article:
An event is similar to a trigger. However, rather than running in
response to a data change, events can be scheduled to run any number
of times during a specific period. In effect, it’s a database-only
cron job.
Pondering this, I'd envision a procedure that deleted anything >1hr (if that's the expiration). This procedure would be TRIGGERED on new inserts to get rid of anything expired at that moment, but also in an event to run every 15 minutes or so so that automatic deletes by the trigger aren't dependant on somebody else adding a reservation to trigger that procedure.
If your server is linux, you can use cron jobs to check once a day every reservation dates. If these dates have expired .. modified field reserves to be available.
Normally I would do it this way:
when storing a reservation, store date_from and date_to both of datatype DATETIME
when checking if there is a computer free check for all computers and filter with WHERE '{$my_date}' >= date_to AND '{$my_date}' <= date_from - by this You should be able to get all the PCs that are not reserved within a certain time...
To be complete in the solution, you need to run a CRON job which calls a query to remove all reservations that have a reservation_time + (15 * 60) < unix_timestamp().
I am assuming you have a time that the reservation was placed or started and are using UNIX/Epoch Timestamps.
Instead of doing a expires_now, if you know it will always be a fixed interval ie 15 minutes, you can do:
DELETE FROM reservations WHERE reservation_time + (15 * 60) < unix_timestamp()
Something you could look into is managing cron job's from PHP, http://www.highonphp.com/cron-job-manager.
The above script will, when a reservation is created, insert an entry into /etc/cron.d/ and you could configure it to run at the expected reservation endtime. Then inside the php file which would be executed, you could do:
DELETE FROM reservations WHERE id = :id

Categories