Skip to content

Instantly share code, notes, and snippets.

@Jonty
Created April 17, 2009 17:33
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 Jonty/97149 to your computer and use it in GitHub Desktop.
Save Jonty/97149 to your computer and use it in GitHub Desktop.
<?php
function getMiddleDay($day, $month, $year) {
$thisMonth = mktime(0, 0, 0, $month, 0, $year);
$daysInMonth = date('t', $thisMonth);
$middleTimestamp = mktime(0, 0, 0, $month, floor($daysInMonth/2), $year);
$potentials = array();
foreach (array(2,3) as $i) {
$timestamp = strtotime("{$i} {$day}", $thisMonth);
$potentials[abs($timestamp - $middleTimestamp)] = $timestamp;
}
ksort($potentials);
return array_shift($potentials);
}
$timestamp = getMiddleDay('thursday', date('n'), date('Y'));
if (time() > strtotime('10pm', $timestamp)) {
$nextMonth = strtotime('next month');
$timestamp = getMiddleDay(
'thursday',
date('n', $nextMonth),
date('Y', $nextMonth)
);
}
// Next event
print date('d-m-Y',$timestamp)."\n";
// Time remaining
$startTime = strtotime('7pm', $timestamp);
$remaining = $startTime - time();
if ($remaining <= 0) {
print "Currently in progress!\n";
} else {
$days = floor($remaining / 86400);
$remaining -= $days * 86400;
$hours = floor($remaining / 3600);
$remaining -= $hours * 3600;
$minutes = ceil($remaining / 60);
print "{$days} days, {$hours} hour(s), {$minutes} minutes until beer.\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment