Set database value after a certain date has passed - php

I have a database with variables I want to change depending on the date. The events in the database have dates. After this date I would like to set a variable 'past' to true. Meaning, the event has past. How can I approach this with php or is this something that needs to be added to the database itself (using phpMyAdmin) ?

Naturally, you first need code that does the actual update. This could very well be a PHP script, but you probably won't want to embed it into a web page, but to run it from the command line (see below).
Secondly, you need to execute that code on a regular basis so every update is done within a guaranteed time span after the event has passed. To execute scripts or other code regularly, you can use cron (if you are on Linux) and similar mechanisms.
If you need to make sure that the updates are done as fast as possible after the respective event has passed, you will need to write a full-blown daemon which runs in the background and does its checks 10 times per second, for example, or (instead of polling) uses appropriately programmed one-shot timers.
If you insist on writing the script in PHP and don't want to run it from the command line (as required by cron for example), you still can embed your code into a web page as usual and then use tools like wget or curl in conjunction with cron to retrieve that web page and get your code executed regularly.

Related

do php file at a time according to database

I'm creating a website and in my website I have a MySQL database. In the database there is a table that has two property "date" and "time". I want to run a php code on a date and time according to records of table, automatically. How can I do it?
I see three options, listed here in order of difficulty to implement, easiest to hardest.
Create a script that is scheduled to execute every now and then (exact timing would depend on your needs for accuracy), which checks the database to see if there are any items in there that should have been executed in the time span since it's last execution. If there are, execute them.
Write a daemon application/script to monitor your database, and execute the tasks when needed. This one is not all that different from #1 except that it wouldn't require scheduling or manual triggering. That could be handled within the application/script itself.
Create a script that checks the database for changes, and schedules/reschedules/removes any tasks accordingly. This script would then be scheduled to execute every now and then and/or be executed manually by the application that manages the database.
Write a php script (that queries the database and runs the script) powered by crontab (for periodically running your 'query'-script) in a shell (on linux)

Posting data from database at regular time intervals automatically in php

I have questions stored in my database. I want to regularly post one question on my website from the database at a 24 hr interval automatically. Is there a way I can do that ?
You can do this with steps:
Create normal PHP-script which will post your questions.
Schedule your script with standard OS scheduler. It is cron for *nix (Win-versions exist too) or AT for Windows. To define certain interval - you should read scheduler's manual (for cron format is provided here)
Example (cron)
0 2 * * * /usr/bin/php /path/to/insert/script.php
-in this case every day at 02:00 AM cron will try to execute command /usr/bin/php /path/to/insert/script.php - i.e. if your script.php will extract your question from DB and post it - that will do the stuff.
Yes you can do it by using Cron job . Set time interval and your file script location. It will automatically hit your script on that time interval.
Here is good tutorial : http://docs.phplist.com/SetupCronJob.html
Providing you could create a PHP script to select a different question each time, all you'd need to do would be to set up a cron to run the PHP script every 24 hours. You can find more info on cron here.
You should look into MySQL date functions.
A contrived example would be using CURDATE():
SELECT * FROM questions WHERE publish_date = CURDATE()
Storing the publish_date will mean you can dynamically load the question when that date arrives.
The only way to do it correctly is to use cron jobs. You should take a look at the administration panel of your hosting service.
Write a script that will post one question on your website everyday and set a cron job to run that script once a day and you are done.
How to set a cron job , ask you hosting service provider , most of the hosts have this feature in cpanel
Yes, you can. I will shortly outline the two most common solutions. The difficulty rises that PHP is not an always running program, but is a language executed on request and then shutdown on completion.
Have some sort of init.php file on your webserver which is being included on every page. That script will check whether the time has passed since last question, and push a new question.
On the other hand, you can add a cronjob which will execute your php script pushing the question. This solution is more robust, but requires access to a webserver you might not have.
Create a php file put the code to fetch question form your database
then set cronjob to excecute the file on perticular time or also
you can execute file by including it your login or any other page which
lods first by including that php file file so that when first user logs in
it will execute.
Steps
Create a PHP script to select and post a particular question randomly.
In your main php script write an AJAX method(which will load the PHP script) which can be called using setInterval() using the following syntax-
setInterval("AJAX_fun()", 24*3600*1000);
This statement will call the AJAX function in a periodic interval of 24hrs. For that you must know AJAX. I mean what should be the body of the AJAX to load the PHP script that you must have an idea of.
Another alternative
You can simply reload the page using javascript setInterval() function
i.e. <script>setInterval("window.location.reload()", 24*3600*1000);</script> and before that you have to select a question from the database randomly using a PHP logic.

Get a list of dynamic names from a DB and have a cron job that traverses this array (php)

Here's what I'm trying to accomplish in high-level pseudocode:
query db for a list of names (~100)
for each name (using php) {
query a 3rd party site for xml based on the name
parse/trim the data received
update my db with this data
Wait 15 seconds (the 3rd party site has restrictions and I can only make 4 queries / minute)
}
So this was running fine. The whole script took ~25 minutes (99% of the time was spent waiting 15 seconds after every iteration). My web host then made a change so that scripts will timeout after 70 seconds (understandable). This completely breaks my script.
I assume I need to use cronjobs or command line to accomplish this. I only understand the basic us of cronjobs. Any high level advice on how to split up this work in a cronjob? I am not sure how a cronjob could parse through a dynamic list.
cron itself has no idea of your list and what is done already, but you can use two kinds of cron-jobs.
The first cron-job - that runs for example once a day - could add your 100 items to a job queue.
The second cron-job - that runs for example once every minute in a certain period - can check if there are items in the queue, execute one (or a few) and remove it from the queue.
Note that both cron-jobs are just triggers to start a php script in this case and you have two different scripts, one to set the queue and one to process part of a queue so almost everything is still done in php.
In short, there is not much that is different. Instead of executing the script via modphp or fcgi, you are going to execute it via command line php /path/to/script.php.
Because this is a different environment than http, some things obviously don't work. Sessions, cookies, get and post variables. Output gets send to stdout instead of the browser.
You can pass arguments to your script by using $argv.

Triggering code at a certain time

Is it possible to tell PHP to execute a piece of code on a given date and time? For example, Blogger.com allows someone to set a blogpost to be published in the future (e.g. 12/12/14 6:00AM).
Can PHP do something similar?
(Sorry, I don't even know what the correct term for events like these would be to be able to even search for them! :( )
You can do this using a cron job (or scheduled task on Windows); although they are typically used for reoccurring jobs.
If you're using a database, most platforms come with a scheduler.
You can schedule your action in your database and use a cronjob on your server or use a cron job service To run your actions.
https://www.setcronjob.com/
For example when you want to publish your blog in the future, you save your publish date in the future and set some sort of auto-publish bit.
Then every hour a PHP script is ran by a cronjob, this script checks the database for all blogs which need to be published.
It's not possible to tell PHP to do this itself, since it would require a process to run forever to periodically call your PHP code. Thankfully though, there's a couple of things which do this:
1) Call a PHP script from a cron job, which then does any necessary work. If you don't have access to a crontab, you can periodically call this when a user pings your site instead, although that will be less reliable, of course.
2) Use at. This works in basically the same way as cron on Linux systems, but will schedule once and at an exact time.
for "triggering code at a certain time", cron works. But for something as simple as publishing an article at a specific time, it isn't needed. You can just store a publish date with your article. When displaying a list of articles you can adjust your query to something like WHERE PUBLISH_DATE <= NOW() and on the article page check if the article's publish date has passed before showing the article.
On Unix-like systems, there's Cron. You can manage Cron from PHP.
On Windows, there are scheduled tasks - you can also use PHP to manage scheduled tasks.
Be careful with this though - it's kinda hard to test, and you may end up with a schedule that cripples your server.

The best way to schedule a event

is there any other option other than cron to schedule the running of a php backup script at a certain time?. I know you can use php itself to schedule things, but it will only fire if the site is getting traffic.
Are there any other options ?.
Thanks :-)
If you're talking about a database backup, then MySQL 5.1 and above has CREATE EVENT which can be used to trigger events (such as stored procedures that can dump table structure/data to file) at regular intervals or set times
Well, cron jobs is a solution. But not necessary in most cases.
If your script is doing something off the site (like sending an email or something), it must be a cron-job.
But...
I made a textbased rpg-game once where several actions were stored in the database waiting to get triggered at a specified time. I found out that it did not make any difference if the script fired at the time it should, or when the first person visiting the page after the time is beyond the timestamp. You could do these events before displaying the content of the page. (I used a file called monitor, to keep it simple).
Would you like to say more about your "event"?
Unless you feel like writing a daemon/service/etc., cron would be your best bet. If you need a job ran more often than minutely, use a lockfile solution and a looping script.
Well not really, Crons are your best bet.
Other than that call a script, and if certain parameters are met such as time elapsed then run the script.

Categories