Repeating Events
For my current project, I am creating a php script that allows our Instructors to manage their available time so students can view it. I have it up and running just fine, but I was asked to add a few things to it. Basically, when instructors go to add their time, they need to be able to "repeat this every Monday,Tues,Wed etc. for so many times" My current code to do this is below:
$cid = $_SESSION['CID'];
$day = $_POST['day'];
$month = $_POST['month'];
$year = $_POST['year'];
$btime = $_POST['btime'];
$etime = $_POST['etime'];
$rep_mon = $_POST['repeat_day_mon'];
$rep_tues = $_POST['repeat_day_tues'];
$rep_wed = $_POST['repeat_day_wed'];
$rep_thur = $_POST['repeat_day_thur'];
$rep_fri = $_POST['repeat_day_fri'];
$rep_sat = $_POST['repeat_day_sat'];
$rep_sun = $_POST['repeat_day_sun'];
$xrep_day = $_POST['xrepeat_day']; //How many times they want to repeat it
$current_month = date("n");
$tm = mktime(0, 0, 0, $month, $day, $year);
$day_name = date("D",$tm);
if($rep_mon == '1')
{
if($day_name == 'Mon')
{
$counter = 0;
$day1 = $day;
while($counter <= $xrep_day)
{
$q1 = sprintf("INSERT INTO `INS_TIME`(cid, month, day, year, btime, etime)VALUES('%s', '%s','%s', '%s', '%s', '%s')",
mysql_real_escape_string($cid, $con),
mysql_real_escape_string($month, $con),
mysql_real_escape_string($day1, $con),
mysql_real_escape_string($year, $con),
mysql_real_escape_string($btime, $con),
mysql_real_escape_string($etime, $con));
mysql_query($q1, $con) or die("There was an error with the query:" . mysql_error());
$counter++;
$day1 += 7;
}
}
if($day_name == 'Tue')
{
$counter = 0;
$day1 = $day;
while($counter <= $xrep_day)
{
$q1 = sprintf("INSERT INTO `INS_TIME`(cid, month, day, year, btime, etime)VALUES('%s', '%s','%s', '%s', '%s', '%s')",
mysql_real_escape_string($cid, $con),
mysql_real_escape_string($month, $con),
mysql_real_escape_string($day1, $con),
mysql_real_escape_string($year, $con),
mysql_real_escape_string($btime, $con),
mysql_real_escape_string($etime, $con));
mysql_query($q1, $con) or die("There was an error with the query:" . mysql_error());
$counter++;
$day1 += 6;
if($counter >=2)
{
$day1++;
}
}
}
// ...
It continues with more ifs but I didnt want to paste it all in as it is over 1000 lines long. If you need the whole code I can easily get it too you.
We have noticed that sometimes it doesnt work right and will reapeat things more than once, or not at all. I would also like to accomplish the task with shorter code and less ifs.
if you can think of a better way to do this I would be most appreciative!
Thanks!
:::::EDIT::::: New Code that doesnt want to work:
$this_sunday = time() - (date("w") * 86400); // this will give you Sunday
$monday = $this_sunday + 1 * 86400; // 86400 = seconds in a day
$tuesday = $this_sunday + 2 * 86400; // 86400 = seconds in a day
$wednesday = $this_sunday + 3 * 86400; // 86400 = seconds in a day
$thursday = $this_sunday + 4 * 86400; // 86400 = seconds in a day
$friday = $this_sunday + 5 * 86400; // 86400 = se开发者_如何学Goconds in a day
$saturday = $this_sunday + 6 * 86400; // 86400 = seconds in a day
$sunday= $this_sunday + 7 * 86400; // 86400 = seconds in a day
$start_time = mktime($hour, $minute, 0, $month, $day, $year);
//FUNCTION TO DO THE ACTUAL REPEATING
function repeat($start_time, $duration, $repeat)
{
$week = 604800; // 60 * 60 * 24 * 7 (1 week in seconds)
for ($i=0; $i<$repeat; ++$i, $start_time += $week)
{
$date = date("Y-m-d", $start_time);
$sql = sprintf("INSERT INTO STAT_INS_AVAIL(CID,DATE,DURATION)VALUES('%s','%s','%s')",$cid,$date,$duration);
}
}
//REPEAT ON MONDAYS
if($rep_mon == '1')
{
repeat($monday, $duration, $xrep_day);
}
//REPEAT ON TUESDAYS
if($rep_tues == '1')
{
repeat($tuesday, $duration, $xrep_day);
}//REPEAT ON WEDBNESDAYS
if($rep_wed == '1')
{
repeat($wednesday, $duration, $xrep_day);
}//REPEAT ON THURSDAYS
if($rep_thur == '1')
{
repeat($thursday, $duration, $xrep_day);
}//REPEAT ON FRIDAYS
if($rep_fri == '1')
{
repeat($friday, $duration, $xrep_day);
}//REPEAT ON SATURDAYSS
if($rep_sat == '1')
{
repeat($saturday, $duration, $xrep_day);
}//REPEAT ON SUNDAYS
if($rep_sun == '1')
{
repeat($sunday, $duration, 4);
}
//Go through this is they do not want to repeat AT ALL
if(!$rep_mon || !$rep_tues || !$rep_wed || !$rep_thur || !$rep_fri || !$rep_sat || !$rep_sun)
{
$date = date("Y-m-d G:i:s", $start_time);
$sql = sprintf("INSERT INTO STAT_INS_AVAIL(CID,DATE,DURATION)VALUES('%s','%s','%s')",$cid,$date,$duration);
mysql_query($sql,$con);
}
I would do something like this:
function repeat($start_time, $duration, $repeat) {
$week = 604800; // 60 * 60 * 24 * 7 (1 week in seconds)
for ($i=0; $i<$repeat; ++$i, $start_time += $week) {
$date = date("Y-m-d", $start_time);
$sql = "INSERT INTO ... $date, $duration ...";
}
}
$start = mktime($hour, $minute, $second, $month, $day, $year);
// some logic to figure out start times by dates of the week
// ...
repeat($monday, 100, 4);
repeat($tuesday, 100, 4);
And of course I would use a single DATETIME
field to store datetimes, and an INTEGER
to store the duration in minutes (or whatever is more convenient).
EDIT: You could put the scheduling part in a function and call it with different parameters, but you still need logic to figure out the correct start times.
To figure out the start of the week, you can use something like:
$this_sunday = time() - (date("w") * 86400); // this will give you Sunday
$tuesday = $this_sunday + 2 * 86400; // 86400 = seconds in a day
$friday = $this_sunday + 5 * 86400; // 86400 = seconds in a day
精彩评论