function out_of_time($time1, $time2, $instructions_array, $bedTime, $ampm) { $time = abs($time1 - $time2); //depending on the number of day and the number of divisons, we choose different date formats. if (days_between($time1, $time2) > sizeof($div_array)) { $format = "D M d, Y"; $stages = $time1; //keep the running total. for ($i = 0; $i < sizeof($instructions_array); $i++) { $stages += $time * $instructions_array[$i][0]; $dates[$i] = date($format, $stages); } } else { $format = "g a D M d"; $stages = $time1; //keep the running total. for ($i = 0; $i < sizeof($instructions_array); $i++) { $stages += $time * $instructions_array[$i][0]; $hour24 = date("G", $stages); if ($hour24 > 12) { $hour24 = $hour24 - 24; } //this keeps the time centered around midnight. 2 is 2 am and -2 is 10pm if ($ampm == am && $hour24 > $bedTime && $hour24 < $bedTime + 10 or $ampm == pm && $hour24 > $bedTime - 12 && $hour24 < $bedTime - 2) { //if the time is later than a person wants to get to sleap $dates[$i] = "{$bedTime} {$ampm} " . date("D M d", $stages); } else { $dates[$i] = date($format, $stages); } } } return $dates; }
function process_form() { global $vars, $phpcdb, $phpc_cal, $phpcid, $phpc_script, $phpc_user; // When modifying events, this is the value of the checkbox that // determines if the date should change $modify_occur = !isset($vars['eid']) || !empty($vars['phpc-modify']); if ($modify_occur) { $start_ts = get_timestamp("start"); $end_ts = get_timestamp("end"); switch ($vars["time-type"]) { case 'normal': $time_type = 0; break; case 'full': $time_type = 1; break; case 'tba': $time_type = 2; break; default: soft_error(__("Unrecognized Time Type.")); } $duration = $end_ts - $start_ts; if ($duration < 0) { message(__("An event cannot have an end earlier than its start.")); return display_form(); } } verify_token(); if (0) { permission_error(__('You do not have permission to write to this calendar.')); } if ($phpc_cal->can_create_readonly() && !empty($vars['readonly'])) { $readonly = true; } else { $readonly = false; } $catid = empty($vars['catid']) ? false : $vars['catid']; if (!isset($vars['eid'])) { $modify = false; $eid = $phpcdb->create_event($phpcid, $phpc_user->get_uid(), $vars["subject"], $vars["description"], $readonly, $catid); } else { $modify = true; $eid = $vars['eid']; $phpcdb->modify_event($eid, $vars['subject'], $vars['description'], $readonly, $catid); if ($modify_occur) { $phpcdb->delete_occurrences($eid); } } if ($modify_occur) { $oid = $phpcdb->create_occurrence($eid, $time_type, $start_ts, $end_ts); $occurrences = 1; switch ($vars["repeats"]) { case "never": break; case 'daily': if (!isset($vars["every-day"])) { soft_error(__("Required field \"every-day\" is not set.")); } $ndays = $vars["every-day"]; if ($ndays < 1) { soft_error(__("every-day must be greater than 1")); } $daily_until = get_timestamp("daily-until"); while ($occurrences <= 730) { $start_ts = add_days($start_ts, $ndays); $end_ts = add_days($end_ts, $ndays); if (days_between($start_ts, $daily_until) < 0) { break; } $phpcdb->create_occurrence($eid, $time_type, $start_ts, $end_ts); $occurrences++; } break; case 'weekly': if (!isset($vars["every-week"])) { soft_error(__("Required field \"every-week\" is not set.")); } if ($vars["every-week"] < 1) { soft_error(__("every-week must be greater than 1")); } $ndays = $vars["every-week"] * 7; $weekly_until = get_timestamp("weekly-until"); while ($occurrences <= 730) { $start_ts = add_days($start_ts, $ndays); $end_ts = add_days($end_ts, $ndays); if (days_between($start_ts, $weekly_until) < 0) { break; } $phpcdb->create_occurrence($eid, $time_type, $start_ts, $end_ts); $occurrences++; } break; case 'monthly': if (!isset($vars["every-month"])) { soft_error(__("Required field \"every-month\" is not set.")); } if ($vars["every-month"] < 1) { soft_error(__("every-month must be greater than 1")); } $nmonths = $vars["every-month"]; $monthly_until = get_timestamp("monthly-until"); while ($occurrences <= 730) { $start_ts = add_months($start_ts, $nmonths); $end_ts = add_months($end_ts, $nmonths); if (days_between($start_ts, $monthly_until) < 0) { break; } $phpcdb->create_occurrence($eid, $time_type, $start_ts, $end_ts); $occurrences++; } break; case 'yearly': if (!isset($vars["every-year"])) { soft_error(__("Required field \"every-year\" is not set.")); } if ($vars["every-year"] < 1) { soft_error(__("every-month must be greater than 1")); } $nyears = $vars["every-year"]; $yearly_until = get_timestamp("yearly-until"); while ($occurrences <= 730) { $start_ts = add_years($start_ts, $nyears); $end_ts = add_years($end_ts, $nyears); if (days_between($start_ts, $yearly_until) < 0) { break; } $phpcdb->create_occurrence($eid, $time_type, $start_ts, $end_ts); $occurrences++; } break; default: soft_error(__("Invalid event type.")); } } if ($eid != 0) { if ($modify) { $message = __("Modified event: "); } else { $message = __("Created event: "); } /* before return message_redirect(tag($eid, $message, create_event_link('', 'display_event', $eid)), */ return message_redirect(tag('', $message, create_event_link('', 'display_event', '')), "{$phpc_script}?action=display_event&phpcid={$phpcid}&oid={$oid}"); /* <-- before last paremeter was &eid=$eid instead of &oid=$oid */ } else { return message_redirect(__('Error submitting event.'), "{$phpc_script}?action=display_month&phpcid={$phpcid}"); } }
function days_between($ts1, $ts2) { // First date always comes first if ($ts1 > $ts2) { return -days_between($ts2, $ts1); } // If we're in different years, keep adding years until we're in // the same year if (date('Y', $ts2) > date('Y', $ts1)) { return days_in_year_ts($ts1) + days_between(add_years($ts1, 1), $ts2); } // The years are equal, subtract day of the year of each return date('z', $ts2) - date('z', $ts1); }
/********************************************************** File: date.php Modified: JG Last Modified: 12.19.2005 *********************************************************** Comments: Modified to allow register_globals to be turned off. **********************************************************/ include "instructions.php"; $today = date("Ymd"); //set up the times $date1 = "{$_GET['monthone']}/{$_GET['dayone']}/{$_GET['yearone']}"; $date2 = "{$_GET['monthtwo']}/{$_GET['daytwo']}/{$_GET['yeartwo']}"; $time1 = strtotime($date1) + 43200; $time2 = strtotime($date2); $days = days_between($time1, $time2); $dates = out_of_time($time1, $time2, $instructions, $bedTime, $ampm); pres_date(one, date("j"), date("n"), date("Y")); pres_date(two, "", "", date("Y")); for ($i = 0; $i < sizeof($instructions); $i++) { $j = $i + 1; $datum = date("Ymd", strtotime($dates[$i])); } for ($i = 0; $i < sizeof($instructions); $i++) { $j = $i + 1; //hacked in because someone forgot the zero item in the array... //description $datum = date("Ymd", strtotime($dates[$i])); //what should be done for ($j = 0; $j < sizeof($instructions[$i][2]); $j++) { }
function add_repeat_defaults($occs, &$defaults) { // TODO: Handle unevenly spaced occurrences $defaults['repeats'] = 'never'; if (sizeof($occs) < 2) { return; } $event = $occs[0]; $day = $event->get_start_day(); $month = $event->get_start_month(); $year = $event->get_start_year(); // Test if they repeat every N years $nyears = $occs[1]->get_start_year() - $event->get_start_year(); $repeats_yearly = true; $nmonths = ($occs[1]->get_start_year() - $year) * 12 + $occs[1]->get_start_month() - $month; $repeats_monthly = true; $ndays = days_between($event->get_start_ts(), $occs[1]->get_start_ts()); $repeats_daily = true; for ($i = 1; $i < sizeof($occs); $i++) { $cur_occ = $occs[$i]; $cur_year = $cur_occ->get_start_year(); $cur_month = $cur_occ->get_start_month(); $cur_day = $cur_occ->get_start_day(); // Check year $cur_nyears = $cur_year - $occs[$i - 1]->get_start_year(); if ($cur_day != $day || $cur_month != $month || $cur_nyears != $nyears) { $repeats_yearly = false; } // Check month $cur_nmonths = ($cur_year - $occs[$i - 1]->get_start_year()) * 12 + $cur_month - $occs[$i - 1]->get_start_month(); if ($cur_day != $day || $cur_nmonths != $nmonths) { $repeats_monthly = false; } // Check day $cur_ndays = days_between($occs[$i - 1]->get_start_ts(), $occs[$i]->get_start_ts()); if ($cur_ndays != $ndays) { $repeats_daily = false; } } $defaults['yearly-until-date'] = "{$cur_month}/{$cur_day}/{$cur_year}"; $defaults['monthly-until-date'] = "{$cur_month}/{$cur_day}/{$cur_year}"; $defaults['weekly-until-date'] = "{$cur_month}/{$cur_day}/{$cur_year}"; $defaults['daily-until-date'] = "{$cur_month}/{$cur_day}/{$cur_year}"; if ($repeats_daily) { // repeats weekly if ($ndays % 7 == 0) { $defaults['repeats'] = 'weekly'; $defaults['every-week'] = $ndays / 7; } else { $defaults['every-week'] = 1; // repeats daily $defaults['repeats'] = 'daily'; $defaults['every-day'] = $ndays; } } else { $defaults['every-day'] = 1; $defaults['every-week'] = 1; } if ($repeats_monthly) { $defaults['repeats'] = 'monthly'; $defaults['every-month'] = $nmonths; } else { $defaults['every-month'] = 1; } if ($repeats_yearly) { $defaults['repeats'] = 'yearly'; $defaults['every-year'] = $nyears; } else { $defaults['every-year'] = 1; } }
function process_form() { global $vars, $phpcdb, $phpc_script, $phpc_user, $phpc_cal; // When modifying events, this is the value of the checkbox that // determines if the date should change $modify_occur = !isset($vars['eid']) || !empty($vars['phpc-modify']); if ($modify_occur) { $start_ts = get_timestamp("start"); $end_ts = get_timestamp("end"); switch ($vars["time-type"]) { case 'normal': $time_type = 0; break; case 'full': $time_type = 1; break; case 'tba': $time_type = 2; break; default: soft_error(__("Unrecognized Time Type.")); } $duration = $end_ts - $start_ts; if ($duration < 0) { throw new Exception(__("An event cannot have an end earlier than its start.")); } } verify_token(); if (!isset($vars['cid'])) { throw new Exception(__("Calendar ID is not set.")); } $cid = $vars['cid']; $calendar = $phpcdb->get_calendar($cid); if (!$calendar->can_write()) { permission_error(__('You do not have permission to write to this calendar.')); } if ($calendar->can_create_readonly() && !empty($vars['readonly'])) { $readonly = true; } else { $readonly = false; } $catid = empty($vars['catid']) ? false : $vars['catid']; if (!isset($vars['eid'])) { $modify = false; $eid = $phpcdb->create_event($cid, $phpc_user->get_uid(), $vars["subject"], $vars["description"], $readonly, $catid); } else { $modify = true; $eid = $vars['eid']; $phpcdb->modify_event($eid, $vars['subject'], $vars['description'], $readonly, $catid); if ($modify_occur) { $phpcdb->delete_occurrences($eid); } } foreach ($phpc_cal->get_fields() as $field) { $fid = $field['fid']; if (empty($vars["phpc-field-{$fid}"])) { if ($field['required']) { throw new Exception(sprintf(__('Field "%s" is required but was not set.'), $field['name'])); } continue; } $phpcdb->add_event_field($eid, $fid, $vars["phpc-field-{$fid}"]); } if ($modify_occur) { $occurrences = 0; $n = 1; $until = $start_ts; switch ($vars['repeats']) { case 'daily': check_input("every-day"); $n = $vars["every-day"]; $until = get_timestamp("daily-until"); break; case 'weekly': check_input("every-week"); $n = $vars["every-week"] * 7; $until = get_timestamp("weekly-until"); break; case 'monthly': check_input("every-month"); $n = $vars["every-month"]; $until = get_timestamp("monthly-until"); break; case 'yearly': check_input("every-year"); $n = $vars["every-year"]; $until = get_timestamp("yearly-until"); break; } if ($n < 1) { soft_error(__('Increment must be 1 or greater.')); } while ($occurrences <= 730 && days_between($start_ts, $until) >= 0) { $oid = $phpcdb->create_occurrence($eid, $time_type, $start_ts, $end_ts); $occurrences++; switch ($vars["repeats"]) { case 'daily': case 'weekly': $start_ts = add_days($start_ts, $n); $end_ts = add_days($end_ts, $n); break; case 'monthly': $start_ts = add_months($start_ts, $n); $end_ts = add_months($end_ts, $n); break; case 'yearly': $start_ts = add_years($start_ts, $n); $end_ts = add_years($end_ts, $n); break; default: break 2; } } } if ($eid != 0) { if ($modify) { $message = __("Modified event: "); } else { $message = __("Created event: "); } return message_redirect(tag('', $message, create_event_link($eid, 'display_event', $eid)), "{$phpc_script}?action=display_event&eid={$eid}"); } else { return message_redirect(__('Error submitting event.'), "{$phpc_script}?action=display_month&phpcid={$cid}"); } }
function showDays($date1, $date2, $format) { $dateArr1 = explode("-", $date1); $dateArr2 = explode("-", $date2); $days = days_between((int) $dateArr2[0], (int) $dateArr2[1], (int) $dateArr2[2], (int) $dateArr1[0], (int) $dateArr1[1], (int) $dateArr1[2]); $ret = showDate($date1, $format); $ret .= '[-]' . showDate($date2, $format); $ret .= ' (' . ($days + 1) . ' ' . getLT('days') . ')'; return $ret; }