Can time() be used to replace cron?

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
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

Thanks in advance.

Re: Can time() be used to replace cron?

deko wrote:

Quoted text here. Click to load it

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!



Re: Can time() be used to replace cron?

deko wrote:

Quoted text here. Click to load it

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 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 /

Chris Hope - The Electric Toolbox - /

Re: Can time() be used to replace cron?

Quoted text here. Click to load it

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.

Re: Can time() be used to replace cron?

Quoted text here. Click to load it

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);
//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.

Site Timeline