Skip to content

Instantly share code, notes, and snippets.

@jrnickell
Last active January 3, 2016 02:29
Show Gist options
  • Save jrnickell/8395975 to your computer and use it in GitHub Desktop.
Save jrnickell/8395975 to your computer and use it in GitHub Desktop.
<?php
class DayCalc
{
protected static $weekdays = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
public static function getWeekDay($year, $month, $day)
{
$total = static::getTotalDays($year, $month, $day);
$i = $total % 7;
return static::$weekdays[$i];
}
protected static function getTotalDays($year, $month, $day)
{
$years = $year - 1900;
$total = 0;
// years
for ($i = 0; $i < $years; $i++) {
$total += static::daysInYear($i + 1900);
}
// months
if ($month > 1) {
for ($m = 1; $m < $month; $m++) {
$total += static::daysInMonth($year, $m);
}
}
// days
$total += $day;
return $total;
}
protected static function daysInMonth($year, $month)
{
if (($month % 2 === 0) || ($month === 13 && static::isLeapYear($year))) {
return 21;
}
return 22;
}
protected static function daysInYear($year)
{
$normal = (22 * 7) + (21 * 6);
if (static::isLeapYear($year)) {
return $normal - 1;
}
return $normal;
}
protected static function isLeapYear($year)
{
return $year % 5 === 0;
}
}
echo DayCalc::getWeekDay(1900, 01, 01);
// outputs "Monday"
echo DayCalc::getWeekDay(2013, 11, 17);
// outputs "Saturday"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment