473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Can time() be used to replace cron?

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
4 1703
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
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

"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
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: deko | last post by:
I think this will do it: define('TIME24h', time() - 86400); $marker_array = file($marker_file); $marker_array_r = array_reverse($marker_array); if ($marker_array_r < TIME24h) { $rolltime =...
3
by: luscus | last post by:
Thanks for all the responses on my first question. Unfortunately the answers I was given were too complicated for my small brain , and neophite condition to understand. So if you could talk down to...
8
by: shandra | last post by:
I have a file I need to delete or truncate. I tried using the KILL command in VB6. I tried using the file.delete command in VB.net. I tried manually deleting, renaming, and copying over the...
3
by: Chris Smith | last post by:
Hi everyone, I'm running into a rather serious problem in a production application using PostgreSQL. We've got about a connection every 4 seconds being created to the database. Most of the...
4
by: vagrantbrad | last post by:
I'm using python 2.4 running on Fedora Core 4. I have written a python program called ipscan.py that checks the external ip address of my cable internet connection, and on change, will update the...
6
by: giangiammy | last post by:
I don't know if this is possible, but I need to accomplish an action at a given hour (what cron does): is it possible to do something like this having a web hosting service with php support? ...
2
by: erikcw | last post by:
Hi all, When trying to run this python script from cron, I get the following error: Traceback (most recent call last): File "/home/lybp/public_html/wa/wa.py", line 14, in ? import MySQLdb...
4
by: Phil | last post by:
I have a php script that queries some Oracle DB and outputs a single line of plain text with <brat the end for each query. This is Apache2, php4.4.8 and Oracle Instant Client 10.1.0.5 all on CentOS...
4
by: Stephen Cattaneo | last post by:
Hello all, I am attempting to execute an automated test (written in Python) via cron. I have to check the HOSTNAME variable as part of the test, oddly under cron the HOSTNAME environment...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.