how to execute some function on specific time implicitly - php

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

Related

Run timed PHP function via SQL

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.

Writing time dependant php scripts for server

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

Account activation time out in PHP & general PHP help

I was wondering because I can code in PHP without minors hitches or errors but one thing I have never understood about PHP is the fact it is a server side scripting language, so it needs something to trigger it, if this is so how can I create a PHP file which runs routine checks? and example is:
Someone has not activated there account although they have had a email sent out to them and it has been the time period given, were and how would the script run, would it need to be required into a page or is there a function for this.
Thanks in advance, I just need this clearing up a little it if's not too much trouble.
For this you would usually set up a cron job.
cron is a daemon that periodically starts other programs, for tasks such as the one you outline. You can make cron start php scripts, as well.
If you're on shared hosting, there's probably an option somewhere in your control panel to set up a periodic cron job.
Otherwise, here are some guides on doing it manually:
Cron Tutorial – Managing cron tab or cron job is Easy!
Newbie: Intro to cron
Cron Tutorial
Running PHP Scripts with Cron
If you are on a Unix/Linux system, you can write a php script that can be executed from command-line and add it as a cron job.

automatic run php code without user control

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.

Resetting a MySQL Field value without user execution

I need to reset a MySQL Field value automatically at midnight. It is a specific column in a specific row in a table. I know how to do this in PHP but I do not know how to execute the PHP Script at midnight without someone having to do it themselves. Do you have any viable solutions?
Edit:--------------------
Preferably without using Cron Jobs.
If you are running on linux you would use a cronjob
On most distros there is a command called crontab that schedules tasks for you and a specific format you need:
0 0 * * * php /path/to/file.php
EDIT:
Wrote this before you edited yours :p I'm not sure there is any other way. Do you have a specific reason not to use a cronjob?
You may not even need to specify the php part if the php file is marked as executable i.e
0 0 * * * /path/to/file.php
Just do "chmod +x /path/to/file.php" form the linux command line
There are web based cron services too. Basically you set up an account and then they visit an URL of your choosing at a regular interval. On your server you set up a page that does what you need to get done. Preferably give it a unlikely-to-find-name like myjob789273634ahhh8s2nhw8sghusgf874wfu.php. You get the idea. (Remember that PHP-scripts timeout after like 30secs.)
Here's a google search:
**oops I'm new so I can't post URL apparently. Just search for "web based cron".
Good luck
/0
You could write a job scheduler into your program that runs jobs in a cron-like way. It would require a user to interact with the system to trigger, but it might be good enough depending on your needs. This is much more complicated than just running a cronjob, and does not ensure prefect timing (since it wont run until a user hits a page).
You'd probably need to add a table into you database that would list the job, the time you want them done, and a locking flag to avoid concurrent attempts to run the job. Each time your script runs, you'd check this table for overdue jobs and run them as needed.
Asking how to reliably set off a script at the same time every night without cron (or a scheduled task, on Windows) is like asking how to make a dynamic website without a server-side language.
If your app absolutely relies on a script running exactly at midnight, cron is a requirement. If your users have a hosting company that (stupidly) does not permit cron, they're going to be out of luck.

Categories