我有一个预订工具,可以安排重复的活动。我已经能够计算出每天和每周的重复,但由于一个月中的天数不同,我挂断了每月重复。
例如,目标是在每个月的第三个星期四重复预订。我以为我可以通过使用一些简单的数学并以 28 的间隔安排它,但这不起作用。我假设我的代码需要一些额外的变量。
我有一个表格可以传递我的数值,并且就像我提到的每天和每周重复一样。你会看到我通过了 28 作为我的每月值,但会导致问题。它确实在同一天重复,但随着时间的推移在错误的一周内重复。
<input type="radio" class="radio" name="recurring_span" value="1" checked="checked"/><img src='images/icons/calendar-select-days-span.png' alt='daily' title='Daily'>
<input type="radio" class="radio" name="recurring_span" value="7"><img src='images/icons/calendar-select-days.png' alt='weekly' title='Weekly'/>
<input type="radio" class="radio" name="recurring_span" value="28"><img src='images/icons/calendar-select-days.png' alt='montly' title='Monthly'/>
</p>
处理 php 如下(对不起,如果我包含太多,但正如我所说的 PHP 不是很好,认为多比少好):
//store recurring reservation
if ($recurring_date > $reservation_date){
$repeatid = querySQL('res_repeat');
$keys[] = 'repeat_id';
$values[] = "'".$repeatid."'";
}
// UNIX time
$res_dat = mktime(0,0,0,(int)$m1,(int)$d1,(int)$y1);
$recurring_date = mktime(0,0,0,(int)$m2,(int)$d2,(int)$y2);
$recurring_date = ($recurring_date<$res_dat) ? $res_dat : $recurring_date;
// daily or weekly recurring?
$recurring_span = ($_POST['recurring_span']) ? $_POST['recurring_span'] : 1;
//cut both " ' " from reservation_pax
$res_pax = substr($_SESSION['reservation_pax'], 0, -1);
$res_pax = substr($_SESSION['reservation_pax'], 1);
// check if pax not '0'; prevent 'Christof Keller' bug
if ($res_pax < 1) {
$res_pax = 1;
}
//cut both " ' " from reservation_time
$startvalue = $_SESSION['reservation_time'];
$startvalue = substr($startvalue, 0, -1);
$startvalue = substr($startvalue, 1);
// do not subtract pax and table when reservation is moved
//$res_pax = ($_SESSION['outletID'] == $_POST['old_outlet_id']) ? $res_pax : $res_pax*2;
//$res_tbl = ($_SESSION['outletID'] == $_POST['old_outlet_id']) ? 1 : 2;
// main loop to store all reservations ( one or recurring)
while ( $res_dat <= $recurring_date) {
// build new reservation date
$index = '';
$index = array_search('reservation_date',$keys);
// build for availability calculation
$_SESSION['selectedDate'] = date('Y-m-d',$res_dat);
if($index){
$values[$index] = "'".$_SESSION['selectedDate']."'";
}else{
$keys[] = 'reservation_date';
$values[] = "'".$_SESSION['selectedDate']."'";
}
$index = '';
$index = array_search('reservation_wait',$keys);
if($index){
$values[$index] = '1';
}
//Check Availability
// =-=-=-=-=-=-=-=-=
// get Pax by timeslot
$resbyTime = reservationsByTime('pax');
$tblbyTime = reservationsByTime('tbl');
// get availability by timeslot
$occupancy = getAvailability($resbyTime,$general['timeintervall']);
$tbl_occupancy = getAvailability($tblbyTime,$general['timeintervall']);
$val_capacity = $_SESSION['outlet_max_capacity']-$occupancy[$startvalue];
$tbl_capacity = $_SESSION['outlet_max_tables']-$tbl_occupancy[$startvalue];
if( $res_pax > $val_capacity || $tbl_capacity < 1 ){
//prevent double array entry
$index = array_search('reservation_wait',$keys);
if($index>0){
if ($values[$index] == '0') {
// error on edit entry
$_SESSION['errors'][] = date($general['dateformat'],strtotime($_SESSION['selectedDate']))." "._wait_list;
}
$values[$index] = '1'; // = waitlist
}else{
// error on new entry
$keys[] = 'reservation_wait';
$values[] = '1'; // = waitlist
$_SESSION['errors'][] = date($general['dateformat'],strtotime($_SESSION['selectedDate']))." "._wait_list;
}
}
// END Availability
// number of database fields
$max_keys = count($keys);
// enter into database
// -----
$query = "INSERT INTO `$dbTables->reservations` (".implode(',', $keys).") VALUES (".implode(',', $values).") ON DUPLICATE KEY UPDATE ";
// Build 'on duplicate' query
for ($i=1; $i <= $max_keys; $i++) {
if($keys[$i]!=''){
$query .= $keys[$i]."=".$values[$i].",";
}else{
$max_keys++;
}
}
// run sql query
//echo "Query: ".$query;
$query = substr($query,0,-1);
$result = query($query);
$new_id = mysql_insert_id();
$_SESSION['result'] = $result;
// setup the right ID
if( isset($new_id) || $new_id != $_POST['reservation_id']){
$history_id = $new_id;
}else{
$history_id = $_POST['reservation_id'];
}
// store changes in history
$result = query("INSERT INTO `$dbTables->res_history` (reservation_id,author) VALUES ('%d','%s')",$history_id,$_POST['reservation_booker_name']);
// -----
// increase reservation date one day or week
$d1 += $recurring_span;
$res_dat = mktime(0,0,0,$m1,$d1,$y1);
} // end while: reservation to store
我不应该我对 PHP 不太了解并尝试学习,我的经验仅限于处理现有代码的绊脚石。对于这个项目,我一直在努力定制和修改一个开源调度工具以满足我的需要。研究mktime没有太大进展。提前致谢!