This is a discussion on Can time() be used to replace cron? within the PHP Language forums, part of the PHP Programming Forums category; I need code to run once a day (no cron service available) - when the first visitor of the day hits ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
I need code to run once a day (no cron service available) - when the first
visitor of the day hits my site would be fine. What I have in mind is a Boolean function that identifies day boundaries with time() - if I can identify individual day boundaries with time(), then I should be able to identify the first visit of the day - whether it be 12:01am or 1159pm. I assume I'd need to store some value that indicates whether or not the $first_today function has run - I suppose I could write this to a file. So, it would look something like this: if ($first_today) { do stuff; } Has anyone done this before? Other suggestions? (yes, I know, get another host...) Thanks in advance. |
|
|||
|
deko wrote:
> I need code to run once a day (no cron service available) - when the first > visitor of the day hits my site would be fine. > > What I have in mind is a Boolean function that identifies day boundaries > with time() - if I can identify individual day boundaries with time(), > then I should be able to identify the first visit of the day - whether it > be > 12:01am or 1159pm. I assume I'd need to store some value that indicates > whether or not the $first_today function has run - I suppose I could write > this to a file. So, it would look something like this: > > if ($first_today) > { > do stuff; > } Too tricky with time(). It would be easier to use date() eg date('H:i:s') will return the date in HH:MM:SS format, but you're still going to need to store info in a file or the database to see if the script has been done already today. Depending what your script is actually doing, if it's time intensive then they're going to have to wait while the processing is done so that's not such a good solution, and you need to ensure they can't stop it from processing (refer to http://www.php.net/ignore_user_abort for more info about this). As a different solution, I have been considering setting up a "remote cron" type of site where you can log in and get a script to run on your site at a particular time of day. I was thinking of making it free for a single daily request and having some minimal charging structure in place for more complex requests. If you're interested in something like this, feel free to send me an email on my web form at http://www.electrictoolbox.com/article/contact-us/ -- Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/ |
|
|||
|
deko wrote:
> I need code to run once a day (no cron service available) - when the first > visitor of the day hits my site would be fine. > > What I have in mind is a Boolean function that identifies day boundaries > with time() - if I can identify individual day boundaries with time(), > then I should be able to identify the first visit of the day - whether it > be > 12:01am or 1159pm. I assume I'd need to store some value that indicates > whether or not the $first_today function has run - I suppose I could write > this to a file. So, it would look something like this: > If it were me, I'd write the date (dd+mm+yy) the job was done somewhere, then each time the script runs, check if the date on file is different from the current. Don't forget about time zones tho! HTH C. |
|
|||
|
"deko" <www-dot-clearpointsystems-dot-com@nospam.com> wrote in message news:Zrp7d.23081$QJ3.2345@newssvr21.news.prodigy.c om... > I need code to run once a day (no cron service available) - when the first > visitor of the day hits my site would be fine. > > What I have in mind is a Boolean function that identifies day boundaries > with time() - if I can identify individual day boundaries with time(), then > I should be able to identify the first visit of the day - whether it be > 12:01am or 1159pm. I assume I'd need to store some value that indicates > whether or not the $first_today function has run - I suppose I could write > this to a file. So, it would look something like this: > > if ($first_today) > { > do stuff; > } > > Has anyone done this before? Other suggestions? (yes, I know, get another > host...) > > Thanks in advance. > > to get the day identified you can do: $day = date("Ymd",time()); this should put 20041005 into $day, which then can be tested against the datestamp of the file you touch. If you are already creating files then no need to touch, test the date on that file instead. So now, when the script is run by a visitor, it sets the $day value, checks its test file for the date it was last modified, and if the day id is different, run all the daily tasks you want to run. Hope it helps. |
|
|||
|
> to get the day identified you can do:
> $day = date("Ymd",time()); > > this should put 20041005 into $day, which then can be tested against the > datestamp of the file you touch. If you are already creating files then no > need to touch, test the date on that file instead. > > So now, when the script is run by a visitor, it sets the $day value, checks > its test file for the date it was last modified, and if the day id is > different, run all the daily tasks you want to run. > > Hope it helps. I came up with this, which seems to be working: define('TIME24h', time() - 86400); $marker_array = file($marker_file); $marker_array_r = array_reverse($marker_array); if ($marker_array_r[0] < TIME24h) { $rolltime = $marker_array_r[0] + 86400; while ($rolltime < TIME24h) //if no visits in last 24hrs { $rolltime = $rolltime + 86400; } $fp = fopen($marker_file,"a"); fwrite($fp, $rolltime); fclose($fp); //code that needs to run once a day goes here } The next visit to the site after a 24-hour interval will trigger the code. The marker_file will also serve as a log showing each day the scheduled code was run. Of course this assumes you seed the marker_file with a unix timestamp of your choice, and that you only need your code to run once each day, not at a particular time each day. |