PHP Classes
elePHPant
Icontem

Schedule date: Calculate the schedule of periodic events

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Info   View files View files (3)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings  
2010-04-29 (5 years ago) RSS 2.0 feedNot yet rated by the usersTotal: 529 All time: 5,222 This week: 1,084Up
Version License PHP version Categories  
schedule-date 0.1GNU General Publi...4.0Time and Date
Description Author  

This class can be used to calculate the schedule of periodic events.

It can take a string with a special syntax that defines the when a given event will be repeated.

The class can calculate the last time the event should have occurred and the next time it should occur.

Picture of giuseppe lucarelli
Name: giuseppe lucarelli <contact>
Classes: 5 packages by
Country: Italy Italy
Age: 56
All time rank: 84334 in Italy Italy
Week rank: 1576 Down66 in Italy Italy Down
Innovation award
Innovation award
Nominee: 2x

Details provided by the author  
this class can be used to run/repeat a command or a script automatically at a specific time/date.
i've initially developed it for my mailinglist manager script to enable email scheduling/repeating, but you can use it
for others tasks/programs. i've chosen a different syntax from unix "cron" command because it was too hard for me to
explain how to set up a pattern to postpone/repeat an email.
The simplest way to set up a sample pattern for this class could be:
"2011 12 24 23:59"
this means 2011 as year, 12 as month, 24 as day and 23:59 as hour/minute
a more complex pattern could be:
"2011 6-9 2*mon-wed * 0"
this means 2011 as year, from june to september as month, only first two monday-tuesday-wednsday as day, every hour and
0 as minute.

how to use this class
---------------------

the pattern has this syntax:

yy mm dd hh mm 
or
yy/mm/dd hh:mm 
or
yy	mm	dd	hh	mm 

some valid patterns are, for an english week ('sun','mon','tue','wed','thu','fri','sat')

"2010 * 1*sun,tue 12 00"
"2009/1/15 11:57"
"2009  1-7     *       *       0"
"2009  *       *       *       0"
"2009-2010 1 mon,thu,fri 15 51"
"2009-2010/1/2*sun-tue,thu-sat 15:51"
"2009 2 3 0 51"
"2009-2010 01,03 3*sun,thu * 51"
"2009-2010 01,03 1*sun 0 51"
"2009-2010 01,03 * 11 51"
"2009-2010 3,12 1,4-7 15 51"
"2009-2010 01-02 10 0-23 51"
"2009-2010 01-02 10 11,23 51"
"2009 3,12 wed-fri 11 51"
"2009 * 1,4-7,9-12 11 51"

for an italian version of week ('dom','lun','mar','mer','gio','ven','sab')

"2009-2010 * 1*lun,mar 10 51"

Functions:

GetFirstRun(pattern,startTime)

for example:

<?php
require_once("class.scheduledate.php");

    $sd = new ScheduleDate;
    // if you want to run an italian version enable the row below
    // $sd->week = array('dom','lun','mar','mer','gio','ven','sab');
    $time=$sd->GetFirstRun("2011 * * 8:0",strtotime('now'));
    echo "first run will be at [".date("Y-m-d H:i:s",$time)."]";
?>

the output:
    "first run will be at [2011-01-01 08:00:00]"



GetLastRun()

<?php
require_once("class.scheduledate.php");

    $sd = new ScheduleDate;
    // if you want to run an italian version enable the row below
    // $sd->week = array('dom','lun','mar','mer','gio','ven','sab');
    $time=$sd->GetFirstRun("2011 * * 8:0",strtotime('now'));
    echo nl2br("first run will be at [".date("Y-m-d H:i:s",$time)."]\n");

    $time = $sd->GetLastRun();
    echo nl2br("last run will be at [".date("Y-m-d H:i:s",$time)."]\n");
?>

the output:
    "first run will be at [2011-01-01 08:00:00]"
    "last run will be at [2011-12-31 08:00:00]"

Renew(pattern,previousRunDate,timecheck)



<?php
require_once("class.scheduledate.php");

    $sd = new ScheduleDate;
    // if you want to run an italian version enable the row below
    // $sd->week = array('dom','lun','mar','mer','gio','ven','sab');
    $previousRunDate = "2011-1-1 8:0:0"; // this is the last expired valid date
    echo nl2br("previuos run date is [".date("Y-m-d D H:i:s",strtotime($previousRunDate))."]\n");
    $time = $sd->Renew("2011 * 2*wed,sat 8:0", $previousRunDate, strtotime('now'));
    if($time !== false) {
        $date = $sd->date;
        echo "renewed date to [".date("Y-m-d D H:i:s",$time)."] ";
    } else {
        echo "expired\n";
    }
?>

the output:
    "previuos run date is [2011-01-01 Sat 08:00:00]"
    "renewed date to [2011-01-05 Wed 08:00:00]"




if you want a real example of this class have a look of my script "pop3ml" to manage mailing lists with only a
POP3 account (and without a local smtp server)

Download both at http://www.phpclasses.org

Giuseppe Lucarelli
giu.lucarelli@gmail.com


enjoy!

  Files folder image Files  
File Role Description
Plain text file class.scheduledate.php Class main class script
Accessible without login Plain text file readme Doc. small documentation and samples
Plain text file schedulesample.php Example sample script

 Version Control Unique User Downloads Download Rankings  
 0%Total:529All time:5,222
 This week:0This week:1,084Up