Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dopiaza
Created December 7, 2014 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dopiaza/bea3049c02360ec3076b to your computer and use it in GitHub Desktop.
Save dopiaza/bea3049c02360ec3076b to your computer and use it in GitHub Desktop.
PHP class to get the the current game date for the much-missed game Glitch
<?php
namespace Lumen\Glitch\CalendarBundle\Service;
class GlitchCalendar
{
private $timezone;
private $epoch;
private $daysInYear;
private $months = array
(
array('name' =>'Primuary', 'length' => 29, 'holidays' => array(5 => 'AlphCon')),
array('name' =>'Spork', 'length' => 3, 'holidays' => array(2 => 'Lemadan')),
array('name' =>'Bruise', 'length' => 53, 'holidays' => array(3 => 'Pot Twoday')),
array('name' =>'Candy', 'length' => 17, 'holidays' => array(2 => 'Root', 3 => 'Root', 4 => 'Root', 11 => 'Sprinkling')),
array('name' =>'Fever', 'length' => 73, 'holidays' => array()),
array('name' =>'Junuary', 'length' => 19, 'holidays' => array(17 => 'Croppaday')),
array('name' =>'Septa', 'length' => 13, 'holidays' => array(1 => 'Belabor Day')),
array('name' =>'Remember', 'length' => 37, 'holidays' => array(37 => 'Zille-o-ween')),
array('name' =>'Doom', 'length' => 5, 'holidays' => array()),
array('name' =>'Widdershins', 'length' => 47, 'holidays' => array()),
array('name' =>'Eleventy', 'length' => 11, 'holidays' => array(11 => 'Recurse Eve')),
array('name' =>'Recurse', 'length' => 1, 'holidays' => array())
);
private $days = array
(
'Fabday',
'Hairday',
'Moonday',
'Twoday',
'Weddingday',
'Theday',
'Fryday',
'Standday',
);
public function __construct()
{
// Glitch is born!
$this->timezone = new \DateTimeZone('UTC');
$this->epoch = 1238562000;
$this->creationDay = 0; // Fabday
$this->daysInYear = 0;
foreach ($this->months as $m)
{
$this->daysInYear += $m['length'];
}
}
private function getDayAndMonthFromDayOfYear($d)
{
while ($d > $this->daysInYear)
{
$d -= $this->daysInYear;
}
$n = 0;
foreach ($this->months as $m)
{
$n++;
if ($d - $m['length'] <= 0)
{
// This is the one
break;
}
$d -= $m['length'];
}
return array('day' => $d, 'month' => $n);
}
public function now()
{
$glitchTime = array();
try
{
$now = new \DateTime('now', $this->timezone);
$seconds = $now->getTimestamp() - $this->epoch;
// One glitch day = 4 real hours
// so 1 real second = 6 glitch seconds
$glitchSeconds = $seconds * 6;
$glitchMinutes = (int)floor($glitchSeconds/60);
$glitchHours = (int)floor($glitchMinutes/60);
$glitchDays = (int)floor($glitchHours/24);
$glitchYears = (int)floor($glitchDays/$this->daysInYear);
$dayOfYear = $glitchDays % $this->daysInYear + 1;
$dm = $this->getDayAndMonthFromDayOfYear($dayOfYear);
$month = $dm['month'];
$monthName = $this->months[$month - 1]['name'];
$day = $dm['day'];
$dayOfWeek = ($dayOfYear + ($this->daysInYear - 1) * $glitchYears) % count($this->days);
$dayName = ($dayOfYear == $this->daysInYear) ? "" : $this->days[$dayOfWeek];
$holiday = '';
foreach ($this->months[$month - 1]['holidays'] as $hday => $hname)
{
if ($hday == $day)
{
$holiday = $hname;
break;
}
}
$glitchTime = array
(
'second' => $glitchSeconds % 60,
'minute' => $glitchMinutes % 60,
'hour' => $glitchHours % 24,
'dayOfYear' => $dayOfYear,
'dayOfWeek' => $dayOfWeek,
'dayName' => $dayName,
'month' => $month,
'monthName' => $monthName,
'day' => $day,
'year' => $glitchYears,
'holiday' => $holiday,
);
$glitchTime['formatted'] = $this->formatGlitchDateTime($glitchTime);
}
catch (\Exception $e)
{
// Do nothing. It's not important right now.
}
return $glitchTime;
}
public function formatGlitchDateTime($d)
{
$ordinal = "";
switch ($d['day'] % 10)
{
case 1:
if ($d['day'] == 11)
{
$ordinal = 'th';
}
else
{
$ordinal = 'st';
}
break;
case 2:
if ($d['day'] == 12)
{
$ordinal = 'th';
}
else
{
$ordinal = 'nd';
}
break;
case 3:
if ($d['day'] == 13)
{
$ordinal = 'th';
}
else
{
$ordinal = 'rd';
}
break;
default:
$ordinal = 'th';
break;
}
$hour = $d['hour'] % 12;
if ($hour == 0)
{
$hour = 12;
}
$ampm = $d['hour'] < 12 ? 'am' : 'pm';
return sprintf("%d:%02d %s, %s, %d%s of %s, year %d", $hour, $d['minute'], $ampm, $d['dayName'], $d['day'], $ordinal, $d['monthName'], $d['year']);
}
public function daysInYear()
{
return $this->daysInYear;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment