PHP set interval of a process - php

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

Related

MySQL select where timenow > database time

I have a PHP script which creates a new order and inserts the time NOW + 15 minutes into the database.
After 15 minutes have expired the user cannot access the order page.
On the admin panel I only want to show records that have not expired, so my question is what select statement would I use to do this, and which would be the most efficent?
SELECT * FROM `orders` WHERE datenow > date
The "date" is the date of the order + 15 minutes
P.S.
Aternativly, is there any way to do this using PHP so I don't have to put too much stress on the database?
Many Thanks!
Given that you want records that have not yet expired, you want all records where date is greater than NOW().
SELECT
*
FROM
`orders`
WHERE
`date` > NOW()
As the comments mentioned, it is usually better to handle this on the database side; rather than transfer (assuming you have split database and web servers) the entire data set to filter it in the application code.
The other benefit of having all the datetime functions be on the same server (in the database in this case), is that especially on some shared hosting environments, the timezone for the database cannot be changed. Therefore, you want the time comparisons to occur in the same timezone.

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))

Delete a post after a period of time

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)

PHP MySQL hide rows more than 1 minute old

I am trying to make a PHP chat script and I want to make sure it doesn't display any entries older than 1 minute. The database contains a timestamp on each row called timestamp.
How can I do this?
SQL
SELECT *
FROM tablename
WHERE timestamp > TIMESTAMP(DATE_SUB(NOW(), INTERVAL 1 MINUTE))
It's been a while since I've used that particular function, so I might be a bit off but it's close.
JavaScript
It sounds like you might want to actively hide old messages. In that case you need to include the timestamp meta-data so you can filter it using JavaScript.

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