<?php
/*=======================================================================
 *
 * inc_calendar.inc.php
 *
 * Autor: Marc Vollmann
 * 
 =======================================================================*/
 

// Definiton of global parameters

$calWeekDays = getColVectorFromDB2Array("metatype","mt_type","cal_week_days","mt_value","mt_sort");
$calMonthNames = getColVectorFromDB2Array("metatype","mt_type","cal_months","mt_value","mt_sort");

$calCurrentDayNumOfTheYear = date("z", mktime(0, 0, 0, $selMonth, $selDay, $selYear));   // 0 .. 365
$calCurrentDayNumOfTheYear++;  // Increment ...
$calCurrentDayNumOfTheWeek = date("w", mktime(0, 0, 0, $selMonth, $selDay, $selYear));   // 0 = Sunday, 1 = Monday, etc.
if ($calCurrentDayNumOfTheWeek == 0) : $calCurrentDayNumOfTheWeek = "7"; endif;          // Reformat to 1 = Monday, ... , 7 = Sunday
$calCurrentWeekNum = date("W", mktime(0, 0, 0, $selMonth, $selDay, $selYear));           // Calendar week
$calCurrentWeekName = "KW";                                                              // Calendar week name


$calMonthDays = array("31","28","31","30","31","30","31","31","30","31","30","31");
$calCurrentYearIsLeapYear = date("L", mktime(0, 0, 0, $selMonth, $selDay, $selYear));
if ($calCurrentYearIsLeapYear == "1") : $calMonthDays[1] = "29"; endif;    

$minuteArray = array("00","05","10","15","20","25","30","35","40","45","50","55");


// Gets the last calendar week (number) of a specified year
function calLastWeekNumofYear($selYear) {
  $tmpDay = 31;
  while (date("W", mktime(0, 0, 0, 12, $tmpDay, $selYear)) == 1) :
    $tmpDay = $tmpDay -1;
  endwhile;
  $calLastWeekNumOfYear = date("W", mktime(0, 0, 0, 12, $tmpDay, $selYear));
  return $calLastWeekNumOfYear;
};

// Gets the first calendar week (number) of a specified month and year
function calFirstWeekNumofMonth($selMonth, $selYear) {
  // Get calendar week of the 01.mm.YYYY
  $calFirstWeekNumOfMonth = date("W", mktime(0, 0, 0, $selMonth, 1, $selYear));
  return $calFirstWeekNumOfMonth;
};

// Gets the first day (number) of a specified month and year
function calFirstDayNumOfMonth($selMonth, $selYear) {
  $calFirstDayNumOfMonth = date("w", mktime(0, 0, 0, $selMonth, 1, $selYear));
  if ($calFirstDayNumOfMonth == 0) : $calFirstDayNumOfMonth = "7"; endif;
  return $calFirstDayNumOfMonth;
};

// Gets the first day (date array) of the first calendar week of a specified year, it has to be a "Monday"
function calFirstDayNumOfFirstCalendarWeekOfYear($selYear) {
  $tmpDayOffset = 0;
  while (date("W", mktime(0, 0, 0, 1, 1 + $tmpDayOffset, $selYear)) != 1) :
    $tmpDayOffset++;
  endwhile;
  while (date("w", mktime(0, 0, 0, 1, 1 + $tmpDayOffset, $selYear)) != 1) :
    $tmpDayOffset = $tmpDayOffset - 1;
  endwhile;
  $tmpDatetime = mktime(0, 0, 0, 1, 1 + $tmpDayOffset, $selYear);
  return array(date("d", $tmpDatetime), date("m", $tmpDatetime), date("Y", $tmpDatetime));
};

// Gets the first day (date array) of a specified calendar week and year
function calFirstDayNumOfWeek($selWeek, $selYear) {
  $tmpDatetime = calFirstDayNumOfFirstCalendarWeekOfYear($selYear);
  $tmpDatetime = mktime(0, 0, 0, $tmpDatetime[1], $tmpDatetime[0] + (($selWeek - 1) * 7), $tmpDatetime[2]);
  return array(date("d", $tmpDatetime), date("m", $tmpDatetime), date("Y", $tmpDatetime));
};


?>