Gregorian calendar that extends from CustomCalendar

<?php
require ‘CustomCalendar.php’;
/**
* Description of gregorianCalendar
*
* @author David
*/
class CustomGregorianCalendar extends CustomCalendar {

private $months = array();
private $daysOfWeek=array();
/* number of days in every month.
* Thirty days hath September,
* April, June, and November.
* All the rest have thirty-one,
* Excepting February alone,
* Which hath twenty-eight days clear,
* And twenty-nine in each leap year.
*/
/**
* Creates a Gregorian calendar for the year given.<br>
*
* @author David
* @param integer $y
* Takes a 4 digit year to create a calendar for.
* @return nothing
*/
function __construct($y=’2010′) {
print “finished gregorian funciton”;
$daysInMonth = array
(“SEPTEMBER”=>30, “APRIL”=>30, “MAY”=>30, “NOVEMBER”=>30,
“JANUARY”=>31, “FEBRUARY”=>28, “MARCH”=>31, “APRIL”=>31,
“JULY”=>31, “AUGUST”=>31, “OCTOBER”=>31, “DECEMBER”=>31);
$this->daysOfWeek = array
(“SUNDAY”,”MONDAY”,”TUESDAY”,”WEDNESDAY”,”THURSDAY”
,”FRIDAY”,”SATURDAY”);
/* Number of days in each month if not a leap year */
$this->months = array(“JANUARY”, “FEBRUARY”, “MARCH”, “APRIL”, “MAY”, “JUNE”,
“JULY”, “AUGUST”, “SEPTEMBER”, “OCTOBER”, “NOVEMBER”, “DECEMBER”);
$this->setDaysInMonth($daysInMonth);
/* February has 29 days in a leap year */
if($this->isLeapYear($y)) {
$daysInMonth[$months[1]]=29;
}
parent::__construct($y, $daysInMonth);
$this->setFirstDayOfMonths($y);
}

/**
* Determines if year is a leap year.
*
* @author David
* @param integer $year
* Returns true if year is a leap year and false if it is not.
* @return bool
*/
/*
* if year modulo 400 is 0
* then is_leap_year
* else if year modulo 100 is 0
* then not_leap_year
* else if year modulo 4 is 0
* then is_leap_year
* else not_leap_year
*/
function isLeapYear($year) {
/* Year is a leap year */
//        if (!($year%400))
//            return true;
/* Year is not a leap year */
//        if (!($year%100))
//            return false;
/* Year is a leap year */
if (!($year%4))
return true;
/* Year is not a leap year by default */
return false;
}

/**
* Determine on what day of the week the 1st of month starts at.<br>
* returns first 3 letters of day of week.
*
* @author David
* @param integer|string $y,$m
* $y=year of calendar to work with<br>
* $m=first 3 letters of month we are working with<br>
* @return string
*/
function startOfWeek($y, $m=”january”) {
$d=1;
$findDay;
$monthCodes= array
(“January”=>6, “February”=>2, “March”=>2, “April”=>5,
“May”=>0, “June”=>3, “July”=>5, “August”=>1,
“September”=>4, “October”=>6, “November”=>2, “December”=>4);
/*
* 1.  Take the last 2 digits of the year and add a quarter onto
* itself. (04 + 1 = 5)
* 2. Get the corresponding code for the month.
* (January = 6, February = 2, March = 2, etc.
* See month codes for details). July = 5
* 3. Take the day. (=13)
* 4. Add the numbers together (5 + 5 + 13 = 23)
* 5. Take away 7 (or multiples of 7) until a number from 1-7
* is left. (23 – 21 =2)
* 6. This number corresponds to the day of the week.
* (1 = Monday, 2 = Tuesday, etc.) In this case 2 = Tuesday
*/
$findDay=(($y-2000)+(floor(($y-2000)/4)));
$findDay+= $monthCodes[$m]+$d;
if ($findDay>7)
$findDay %= 7;
if($this->isLeapYear($y)) {
if((strcmp($m,”january”)==0)||(strcmp($m,”february”)==0)) {
$findDay-=1;
}
}
return $this->daysOfWeek[$findDay];
}

/**
* Given the year, this function will find the day that the 1st
* lands on for every month.  Week starts on Sunday.  The list is
* stored in Calendar.php as a string and can be retrieved using
* getDayOfWeekStart($m) where $m is month we are looking for.
* @param integer $y
* $y is the 4 digit year stored as an integer
* @return nothing
* @author David
*/
function setFirstDayOfMonths($y) {
$month=array();
foreach($months as $v) {
$month[$v]=$this->startOfWeek($y, $v);
}
$this->setDayOfWeekStart($month);
}

function getMonths() {
print(“in getMonths”);
return $this->months;
}

/**
* Given the date in year, month, day format, this function will determine
* if the date submitted is valid.
* @param string $year, $month, $day
* @return boolean
* @author David
*/
function validDate($year, $month, $day) {
$numberOfDays=1;
if ($month==$months[1]) {
if ($this->isLeapYear($year)) {
$numberOfDays=29;
}
else
$numberOfDays=28;
}
else
$numberOfDays = $this->getDaysInAMonth($month);
if ($numberOfDays>=$day)
return true;
return false;
}

function getDaysOfWeek(){
return $this->daysOfWeek;
}

function convertToArmyTime($hour){
return $hour+12;
}

function convertMonthToNumber($m){
return array_search($m, $this->months)+1;
}

function splitDate($date, $search){
print ‘got here’;
$chunks = explode(‘-‘, $date);
if ($search == ‘year’)
return $chunks[0];
if ($search == ‘month’)
return $chunks[1];
if ($search == ‘day’)
return $chunks[2];
return “error”;
}
}
?>

Leave a Reply