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