PHP Update Entry Every Hour - php

I'm working on a site that generates a dynamic image based on data from another site. The problem is that loading the data from the other site ever time the image is accessed is slow. The Image displays the current stats of a "team" on a tournament website. I want to make a cron job and database that work together to update the info on a specific "team" every hour from when the team was last updated. For example, I could have the following db field:
ID, Name, Url, Wins, Losses, Xp, DateLastUpdated
So with my cron job, I want to update the entries every time the current date is an hour from the date last updated. How should I do this? Is there a specific way I should store the date and time? Should I even use a date and how often should I run the cron job?

If you have a unix system, place a file in /etc/cron.hourly/myjob containing something like:
#!/bin/bash
php /path/to/script.php
or via crontab -e or any interface
01 * * * * php /path/to/sync-script.php
should do the job. It will run sync-script every hour:01

You don't need to store the last time the image was updated; you can simply set your cron job to run hourly. If the cron job is running once per hour and it's the only thing creating the image, you know that the image is exactly 1 hour old at the time your script is invoked.

Related

How to add dynamic cron job for php

I want to run php script at the particular time and then update the time to next interval. Is it possible to set cron job time duration from php script ?
For Example:
1 - set cron to run at the next sunday,
2 - then update the cron time to another particular time from php,
3 - so on..
You can set your cron job to run at a constant interval (every minute, every hour, etc.), and within the job, check if the action needs to be performed. Laravel framework, for example, does this with their scheduler. There is a single entry into the crontab, and within the job itself, it determines what it actually needs to run.
To keep track of the job internally, you can use a database to store the next run time. If the current time is greater than the next run time, you perform the action and update the database based on your rules for determining the next interval.

Truncate database automatically without event scheduler?

My host (blueangelhost.com) claims that I can't use the event scheduler because it takes up too many resources. I have access to cron jobs in cPanel, but I've tried and they don't seem to work.
So, my question: Is there any kind of efficient PHP code that will automatically truncate a MySQL table in a database?
Well, if it needs to be automatic, or at a specific time, not really. But you could have your website trigger the script when someone gets on it, here's the approach you could use:
On a script that is run on every page (header, menu,footer, layout):
Check in DB or file, the date of the last truncate;
If the date is yesterday, run the truncate;
Change DB or file and put current date;
This way, it will execute once a day. But never at the same time, and not if no one walks on your website for a whole day.

How to launch a php script 12 hours after each user triggered event

Any of the users can trigger an event, as many times they wish. Each time this event is triggered, a row in a mysql table is created with the timestamp, their userid and other useful information. What I need to do is launch a script which runs exactly 12 hours after the event is triggered i.e. for each row in the table.
How can I achieve this in an automated and efficient fashion?
You could use a cron job which every minute launches the script.
In the script you should first fetch the row(s) and check if it's OK to run (if 12 hours passed) then continue, else stop.
I don't know if this will be very efficient, it depends on your number of entries in the database, but technically it's not expensive to just check if the current date matches a date fetched from the database + 12 hrs, I cannot say more because you didn't give too much details about your data.
You'd probably be better off with a cronjob
http://en.wikipedia.org/wiki/Cron
Potentially, you could look into MySQL event scheduler. Although It might not fit your needs, hard to really tell on the details given
http://dev.mysql.com/doc/refman/5.1/en/events.html
something like
CREATE EVENT myTimedEvent ON SCHEDULE EVERY INTERVAL 5 minutes DO CALL updateRows();
updateRows checks your criteria (12hours ago), if it is, perform whatever action you want to do. This requires your MySQL to be # version 5.1+ however
You would probably best have a cron job which runs every minute and checks for any rows in the database > 12 hours old that have not yet been processed the script and process them. This wont give you EXACTLY a 12 hour difference, but would give you a difference within a minute of that.
You would probably also want to make sure that script would be able to run within a few seconds such that you don't have overlap of the script running twice at the same time.
This could be done using CronJobs. If you have root access to your Server or a server administration toolkit that offers cronjob managment you would do it on your server. otherwise use an online cronjob service (google for cronjob online).
the cronjob then triggers a php script on your server in your defined interval, like every minute or every 5 minutes.
this script then selects all rows from your mysql table which are older then 12 hours (WHERE timestamp <= NOW() - INTERVAL 12 hour ) and performs your desired actions on each result and delete the result from the table (or mark it done),
just make sure that the fetching and the actions itself are faster than your cronjob interval, otherwise you would have two or more scripts working on the same rows.
The easy way for me is to make the page head contains
<head>
<meta http-equiv="refresh" content="43200"> <!-- 43200 for (12 hours × 60 mintes × 60 seconds) -->
</head>
This method is very helpful to avoid server time out, which you can't avoid if you are using only PHP code
Important if you start the script using submit button, it's recommended to make the form action="other_page" not the same page, and sure you should save your input values as cookie using setcookie function and grab it as a variable in the action page using $cookieVar = $_COOKIE['the_cookie_name'] ;
You may need to increase or decrease the $cookieVar value and update the value again using setcookie every time your head code http-equiv do the refresh automatically after the specific seconds (43200 in our example) depends on what you want to do
Note that if you make your action start automatically without pressing submit button you can do the action in the same page.
I did that idea before, I had 180,000 images in one folder and the server didn't allow me to download it all because it was showing me only first 7998 images, so I create a script to zip each 1000 image in a file outside the images folder and to avoid time out, I made the code refresh each 60 second , finally I got only 180 zip file :)

php: I want a code to be executed every 1 minute

I will try be clear and specific :)
So i have a website, where people purchase stuff from each other.
To keep everything organised, when someone buys something from another user, That purchase will be moved to a table in database, called Pending. (Note: The purchased is not processed yet)
(so all purchases will be moved and queued into this 'Pending' table)
Now what I want to do is, that this table 'Pending', starts processing 1 purchase at a time automatically (let's say 1 purchase every one minute --> if there are any ofcourse).
Now I have no idea how to do this, if someone can atleast give me a hint on what can I use or what to look for, in order to do this.
Note: am using php & mysql
Thanks in advance :D :)
You can write a PHP script to be executed by a cron job. PHP doesn't have to be executed from a browser request, it can be run form the command-line. Cron would make use of this functionality.
The crontab line to execute a command every minute would be as follows:
* * * * * /path/to/script.php
Each column has a * indicating "any value", so this indicates to run the script "any minute, any hour, any day, any month, any day of week"
A cronjob is going to be your best bet, http://en.wikipedia.org/wiki/Cron
Make some file named MovePending.php with a MySQL query that 'moves' the purchases from Pending to Processed, and run it every minute via a cron job.

How to delete mysql row after time passes?

I have no idea where to start with this one:
I have a database that stores postID and Date.
What I want to do is have my website auto delete all rows where Date is less than today. This script can't have any user input at all. No button clicks, nothing. The script must run every day at midnight.
I've been looking all over the place for something that does this and I've found absolutely nothing.
You can use PHP script and use cron job on your cpanel.
Example:
cronjobcommand.php
<?php
include 'your_db_connection';
mysql_query("DELETE FROM your_table_name WHERE Date < NOW()");
?>
I have attached a screenshot below for your more reference.
For those out there who are on a shared hosting, like 1and1's, and can't use cron, here are 2 alternatives :
mysql events enable you to place a time trigger on mysql, which will execute when you'll want, without having to be fired by any kind of user input
if you cannot create mysql events because you're on 1and1 :(, an alternative is to use webcron
You just need to tell webcron the url of the php script you'd like to be run, and they'll trigger it for you at the intervals you want
Why using cronjobs everyday?? Why not filter data on output. For example in your select check if post date equals today with adding a simple where:
SELECT * FROM `posts`
WHERE (DATE(`post_date`) = DATE(NOW()));
This way you're not required to do your database managements/cronjobs on any special time and it will be used just for database managements. Afterwards you can delete unnecessary data at any time using by mysql command like:
DELETE FROM `posts` WHERE (
DATE(`post_date`) < DATE(NOW())
)
Most hosts provide a cron(8) service that can execute commands at specific times. You use the crontab(1) program to manage the crontab(5) file the describes when to run which commands.
There's a lot of functionality available to you, but if you write a program (shell script, php script, C program, whatever) that runs the appropriate MySQL commands, you can call the program via cron(8) in an entirely hands-off fashion.
Run crontab -e to edit your current crontab(5) file. If none exists, hopefully you'll get one with a helpful header. If not, copy this:
# m h dom mon dow command
The columns indicate the minute, hour, day of month, month, and day of week to execute commands. All the numbers in the columns are essentially ANDed together to decide when to run commands.
Thus, midnight every night would look like this:
0 0 * * * /path/to/executable
It's remarkably flexible, so put some time into the documentation, and you'll find many uses for it.
You should set cron job (scheduled tack.) for it.
A cron job is an automated program setup for Linux and Unix operating systems. It allows the user to execute several commands or functions at a specific time and date.
you have cron Job in your cpanel setup. first you need to make a php script with your logic for delete record after each date. take date from server and write script for delete.
then go to cron tab in your cpanel and do settings for time interval to run cron and give path of your php script file.
MySQL doesn't have a task scheduler. So you have to use the task scheduler of your Operating System (CRON under Linux), or to lunch a basic task checker sub-script during the script of the main page (on another page that is supposed to display the changing data).

Categories