By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,722 Members | 1,194 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,722 IT Pros & Developers. It's quick & easy.

Can time() be used to replace cron?

P: n/a
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.
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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/
Jul 17 '05 #2

P: n/a
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.
Jul 17 '05 #3

P: n/a

"deko" <ww*******************************@nospam.com> wrote in message
news:Zr******************@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.

Jul 17 '05 #4

P: n/a
> 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.
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.