Can time() be used to replace cron?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • deko

    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.


  • Chris Hope

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

    deko wrote:
    [color=blue]
    > 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;
    > }[/color]

    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/

    Comment

    • Colin McKinnon

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

      deko wrote:
      [color=blue]
      > 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:
      >[/color]

      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.

      Comment

      • Doug B

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


        "deko" <www-dot-clearpointsyste ms-dot-com@nospam.com> wrote in message
        news:Zrp7d.2308 1$QJ3.2345@news svr21.news.prod igy.com...[color=blue]
        > 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(),[/color]
        then[color=blue]
        > 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[/color]
        another[color=blue]
        > host...)
        >
        > Thanks in advance.
        >
        >[/color]

        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.



        Comment

        • deko

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

          > to get the day identified you can do:[color=blue]
          > $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[/color]
          no[color=blue]
          > 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,[/color]
          checks[color=blue]
          > 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.[/color]

          I came up with this, which seems to be working:

          define('TIME24h ', time() - 86400);
          $marker_array = file($marker_fi le);
          $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_f ile,"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.


          Comment

          Working...