Select Git revision
-
Tim Steiner authored
Creation of framework level controller, model, and view directories. Moved existing library files to /library.
Tim Steiner authoredCreation of framework level controller, model, and view directories. Moved existing library files to /library.
Month.php 2.32 KiB
<?php
class Nmc_Month {
protected $_year;
protected $_month;
public function __construct($year, $month)
{
$this->_year = $year;
$this->_month = $month;
}
public function daysInMonth()
{
$day = 31;
for(; !checkdate($this->_month, $day, $this->_year); $day--);
return $day;
}
public function firstWeekdayOfMonth()
{
$time = strtotime($this->_year . '-' . $this->_month . '-01');
$weekday = date("w", $time);
return $weekday;
}
public function lastWeekdayOfMonth()
{
$day = $this->daysInMonth();
$time = strtotime($this->_year . '-' . $this->_month . '-' . $day);
$weekday = date("w", $time);
return $weekday;
}
public function weeksInMonth()
{
return ceil(($this->firstWeekdayOfMonth() + $this->daysInMonth()) / 7);
}
public function getPreviousMonth()
{
if($this->_month == 1) return new Month($this->_year - 1, 12);
return new Nmc_Month($this->_year, $this->_month - 1);
}
public function getNextMonth()
{
if($this->_month == 12) return new Month($this->_year + 1, 1);
return new Month($this->_year, $this->_month + 1);
}
public function getWeekOfMonth($week)
{
$days = array();
$firstWeekday = $this->firstWeekdayOfMonth();
$firstDay = $week * 7 - $firstWeekday + 1;
if($firstDay < 1) {
$firstDay = $this->getPreviousMonth()->daysInMonth() + $firstDay;
for($i = 0; $i < 7; $i++) {
if($i < $firstWeekday) {
$days[] = array($firstDay + $i, 'previous');
} else {
$days[] = array($i - $firstWeekday + 1, 'current');
}
}
} else if($firstDay + 6 > $this->daysInMonth()) {
$lastWeekday = $this->lastWeekdayOfMonth();
for($i = 0; $i < 7; $i++) {
if($i <= $lastWeekday) {
$days[] = array($i + $firstDay, 'current');
} else {
$days[] = array($i - $lastWeekday, 'next');
}
}
} else {
for($i = 0; $i < 7; $i++) {
$days[] = array($i + $firstDay, 'current');
}
}
return $days;
}
}