Goal: To create a countdown to our next available live stream.
Details: We live stream six times a week all (PST). 1. Sunday at 8:00 a.m. 2. Sunday at 10:00 a.m. 3. Sunday at 12:00 p.m. 4. Sunday at 6:30 p.m. 5. Wednesday at 7:00 p.m. 6. Saturday at 10:00 a.m.
My approach: I check what day it is and what time it is then create the countdown to the next stream.
I'm sure what I have done can be cleaned up and improved, so tell me how.
<?php
// Countdown Stuff
$year = date(Y);
$month = date(m);
$month_day = date(d);
$day = date(w);
$hour = date(G);
$min = date(i);
$addDay = 0;
// It's Sunday
if ( $day == 0 ) {
if ( $hour < 8 ) {
$hour = 8;
$min = 0;
}
else if ( $hour < 10 ) {
$hour = 10;
$min = 0;
}
else if ( $hour < 12 ) {
$hour = 12;
$min = 0;
}
else if ( $hour <= 18 && $min < 30 ) {
$hour = 18;
$min = 30;
}
else {
$addDay = 3;
$hour = 19;
$min = 0;
}
}
// It's Monday
else if ( $day == 1 ) {
$addDay = 2;
$hour = 19;
$min = 0;
}
// It's Tuesday
else if ( $day == 2 ) {
$addDay = 1;
$hour = 19;
$min = 0;
}
// It's Wednesday
else if ( $day == 3) {
if ( $hour < 19 ) {
$hour = 19;
$min = 0;
} else {
$addDay = 3;
$hour = 10;
$min = 0;
}
}
// It's Thursday
else if ( $day == 4 ) {
$addDay = 2;
$hour = 10;
$min = 0;
}
// It's Friday
else if ( $day == 5 ) {
$addDay = 1;
$hour = 10;
$min = 0;
}
// All that's left is Saturday
else {
if ( $hour < 10 ) {
$hour = 10;
$min = 0;
} else {
$addDay = 1;
$hour = 8;
$min = 0;
}
}
$build_date = $year . '-' . $month . '-' . $month_day . ' ' . $hour . ':' . $min . ':00';
$date = new DateTime($build_date);
if ( $addDay ) {
$date->modify("+$addDay day");
}
$date = strtotime($date->format("Y-m-d G:i:s"));
$now = strtotime("now");
$count = $date - $now;
?>
<script type="text/javascript">
var myTime = <?=$count?>;
$('#countdown').countdown({ until: myTime});
</script>