Created 08/2011

Countdown to a time of the day every day

For this question:

I'm looking to try and make this countdown to a time, say 5pm of each day. Is it possible with this?

Result

Time until 5pm: 14:00:52

Source Code:

<?php
function formatTime($unixtime) {
	return date("H:i:s", $unixtime);
}

function formatSeconds($seconds) {
	$time = str_pad(intval(intval($seconds/3600)),2,"0",STR_PAD_LEFT).":"
	. str_pad(intval(($seconds / 60) % 60),2,"0",STR_PAD_LEFT).":"
	. str_pad(intval($seconds % 60),2,"0",STR_PAD_LEFT) ;
	return $time;
}

// explicitly set the timezone
date_default_timezone_set('America/Los_Angeles');
// set the hour according to the rules of strtotime()
$hour_in_english = "5pm";
$passed_message = "{$hour_in_english} has already passed today!";
$future_message = "Time until {$hour_in_english}: ";
$time_now = strtotime("now");
$time_hour = strtotime("today {$hour_in_english}");
$difference_in_seconds = $time_hour - $time_now;
// print $difference_in_seconds;
if ($difference_in_seconds < 0) {
	print $passed_message;
} else {
	print $future_message . formatSeconds($difference_in_seconds);
}?>
blog comments powered by Disqus