Is there a way with PHP to execute PHP functions based on time of day or day of week? I.e. store information to a database every wednesday? That is, without anyone viewing a web page or click a link to check if it wednesday each time? Any help would be great on this. I need a way to read information from a db and write it to a file every so often, i.e. once a week on my server. How can I accomplish these time dependent tasks?
Thanks!
You're looking for crontab (for Linux) or Scheduled Tasks (for Windows). You can execute any PHP file directly from the server by running the PHP executable on it.
(Windows) Example:
php.exe -f P:\ath\to\php\file.php
Your best bet is to set your PHP script to run in Cron.
http://en.wikipedia.org/wiki/Cron
Related
Is there a way to run a PHP file automatically every hour?
Via SQL or some other way.
I'm currently working on a file who's job is to bring information from a table every hour.
This file needs to execute every hour.
Is there any way to do this?
Neither PHP nor MySQL has this ability.
This function is normally available either by your operating system or external tool.
Windows has a job scheduler, or Unix and variants has crontab, where you can specify a command that must be run on a periodic basis.
it is not a build-in facility. however it can be done by some external tools or using some other php code written for it and loaded on the machine
If you have cron on your host, you can use it. Cron runs php script or any other script every time you want to. It's automated so you just put time details in to it and let it do it's magics itself.
First, put your script that you want to run every hour into file. Then search cron, or crontab from your cpanel(or what are you using). Then set time details(if you have those inputs on your cpanel). Then, write to command input: php /your/full/path/here/. If you don't have GUI for setting crons(cron times), you can use this command:
0 * * * * php /your/full/path/here/
Cronjobs has been mentioned in the questions above me. And you should try to find a solution with cronjobs.
But if you can't, you can use the php time_sleep_until function.
The doc is here: http://www.php.net/manual/en/function.time-sleep-until.php
You input a timestamp in the first parameter, and the php script will sleep until given time.
And then you just while it forever. Not a good solution, but it works.
Remember to ignore user abort and set timeout to infinite.
In one of my website i need to insert some row of data in my database every day at a fixed time. I want to do this automatically using sql and php.
How can i do this? please help me.
If your on a webhost, they often use the cPanel system, which has cron jobs build in. Go to the menu, or search for Cron Jobs and add one.
Use the line "php -q ScriptPath" (no quotes)
If you are on a linux system, look into setting up a cron job.
This will enable you to execute a php script that you specify at fixed time intervals.
http://www.linuxweblog.com/crotab-tutorial
You can possibly use cronjob in linux server to do a autotask to run the file once a day
In windows there is also a builtin program called schedule yask that does the same so u can possibly write command .bat file to run a php file in CLI php or id remember if possible to run a specific web adress blabla.com/mussl.php to accomplish what u need..
Can anyone help
I need to run a php file or function on a specific time (atmost predifined).
So i can not find any way to do this , i think about server variable.
This has to be done without browser i mean there is no chance that browser run at that time it may be or may not. I hope you understand the problem pls give solution Thanks.
You need to use cron (on *nix) or scheduled tasks (windows)
http://en.wikipedia.org/wiki/Cron
Most web host software (such as cPanel) allows you to edit the crontab (the list of scheduled tasks) via the web interface
they are called cron jobs
Run a code after specific time in php
I don't know how to call this.
if I wrote a php script to calculate an interest for saving account.
how can I tell the program to run the script on the end of the day without user monitoring. such as 0.00am or the server start.
If you are on UNIX server, Cron is the way to go. On Windows machine use Scheduled tasks.
Call the PHP file via CLI using Crontab, if you're on Windows try pyCron.
you need to run cron jobs look wiki cron for more information. hope this helps.
I have a php script that reads one file through http(the file is on other domain). I would like to read this file only once or twice a day, instead of connecting to it every time the website is refreshed.
Is there any other way than doing it with cron?
I dont want to use cron cause I prefer to setup this behaviour in the script itself .. so it is flexible, so I can use it anywhere without setting up cron every time.
thanks
I've done this kind of thing in the past when I didn't have access to cron:
$lastRunLog = '/path/to/lastrun.log';
if (file_exists($lastRunLog)) {
$lastRun = file_get_contents($lastRunLog);
if (time() - $lastRun >= 86400) {
//its been more than a day so run our external file
$cron = file_get_contents('http://example.com/external/file.php');
//update lastrun.log with current time
file_put_contents($lastRunLog, time());
}
}
If you can't or don't want to use use cron and it's ok to update it only when the page is accessed. You could cache the result of the HTTP request and only update it on a page load it if the cache is older than a day or whatever interval you choose.
You can also use Web Based Cron if you want to hit a site on a timed interval.
You could even use a database table - really simple in structure, id, date, script url, and whatever you need - and add a row every time you run the script.
Then, before run the script simply check the numbers of row for each day you have.
You can use a Cronjob. You can then run the php script by the command line.
php /someplace/somefile.php
The Cronjob would be the following if you update every day.
0 0 * 0 0 php /someplace/somefile.php
Since you explicitly state that you don't want to use cron, the only other way to do this (without something analogous to cron) is to set up your script as a daemon. However, unless you really need the flexibility that daemons provide, cron is much easier and simpler.
Here's one daemon walk-through.
If you're using a Linux distro with systemd:
I had a need for scheduling yearly based jobs, independent of the application (in case the system rebooted or anything like that), and I was given the suggestion to use systemd Timers. The Arch Wiki Page on it gives some examples.
If you are on a *nix environment you can use cron jobs
What's wrong with cron?
You have a couple choices with cron - your php can be invoked by the command line PHP interpreter, or you could use wget or fetch or the equivalent to invoke your PHP on the server.
In general, PHP run from within the context of the web server has a time limit on how long it can execute, so in general you can't set up "background" PHP threads to do stuff "later".