Question:
PHP create scheduled events?
gballz
2010-08-24 18:57:19 UTC
I thought I had asked this before but got no answers and now cant find it in my history.

I want my databases to update in a specific way at 12:01 am daily. The PHP to update the databases the way I want was easy. But I want them to update automatically without me being on the page and that is the problem.
My understanding is something has to happen for PHP to kick in. ie. page load / HTTP POST, etc.
So I tried JavaScript with a time variable and if statement. Which also only seems to be enabled if you actually have the page loaded. So what kind of language and script would I need to execute a PHP script at a set time daily?

I know it is possible, I just dont know the answer.
Four answers:
Rob W
2010-08-24 19:18:08 UTC
If you can't do scheduled tasks, then one possibility (a workaround, really) is to use a log table that tracks when the last update was done. Whenever any page is loaded from the site, check the log table, and see whether or not -- based on the date -- the database needs to be updated. If it does, then you can process the update.



You can also use application variables in conjunction with the log table to prevent needing to go back to the database repeatedly -- once one session in the current application cycle confirms that the database is updated as it needs to be, the application variable can be set, and the system doesn't need to go back anymore while that variable is set (I like to set the date/time that the application next needs to be updated as the variable -- that way, it knows both whether it is set, and if it is set, whether the next refresh is due. Much better than simple boolean.)



It's still a good idea to use log tables, though, as they prevent the system doing everything all over again if the application restarts for any reason.



However - how viable this is as an option is really heavily dependent on the specifics of the solution you're trying to implement, though. If the specific system state at 12:01am is a factor, then this may not work.



If you're using a Windows PC, though, another workaround -- provided that your PC is always on at 12:01, anyway -- would be to set a scheduled task on your local machine that would load the page, whether or not you were there. In Control Panel --> Administrative Tools, you should find something like "task scheduler" or "scheduled tasks". You can set a task to run at 12:01 daily, with a command along the lines of this:



C:\Program Files\Mozilla Firefox\firefox.exe http://www.sitename.com/updatepage.php



This would cause Firefox (or another browser, depending) to fire that page up and trigger the refresh. Again, it's not ideal, as it means your PC needs to be on until then as a minimum, but it's an option if you can't use proper scheduling on the host. I'm not familiar with other OSs, really, so I can't swear to whether or not something similar can be done.
2010-08-24 19:01:59 UTC
Cron jobs are used for automatically running scripts at scheduled times. Depending on where your website is hosted, your host may or may not support cron jobs. If you use a free host, cron jobs will most definitely be limited.



Here's a great guide on them: http://www.thesitewizard.com/general/set-cron-job.shtml
2016-10-18 11:15:23 UTC
Who keeps circulating this distortion of info? look it up on snopes... "...rather, in accordance to Anita McBride, Laura Bush's former chief of team, Mrs. Bush had between 24 and 26 staffers working for her by potential of the top of her husband's 2d term in workplace. it rather is consequently honest to assert that the dimensions of Michelle Obama's team isn't "remarkable," yet rather on a par together with her prompt predecessor's. apart from, in accordance to the linked Press, numerous different First women had greater numbers of workers working for them than Michelle Obama does: a look into some first women and their team sizes: Laura Bush: between 24 and 26 by potential of end of President George W. Bush's term in 2009, in accordance to Anita McBride, Mrs. Bush's chief of team. lady fowl Johnson, whose signature situation became beautifying roadways, had a team of 30, suggested Stacy A. Cordery, a historic previous professor at Montmouth college in Illinois who study first women. Betty Ford had exceedingly much the comparable huge type. Jacqueline Kennedy, who made renovating the White residing house her reason, had approximately 40 human beings on team, Cordery suggested." Yawn...
Richard
2010-08-24 19:05:21 UTC
Use a cron job.



If you are using wamp or xamp see: http://pankajdangi.com/2009/05/crontab-cronjob-on-windows-xampp-wamp/



If you are having your site hosted they should have a section for cron jobs


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...