cc scfoot

Blog

Countdown timer in Browser

For our streaming events websites we have a countdown timer fo the start of the next event.

Unfortunately it didn`t work for overseas viewers due to features of javascript.
We eventually found a solution to show the actual countdown, no matter where you are located.

We have shown an example on the link: Count down example

To vie wthis view one of our streaming sites: View Countdown example

Code to show Date/Time in two zones

This shows the expected event start time, for the UK zone and also the Asia/Singapore timezone.
$UK = date(`Y-m-d H:i:s`) ;
$UK_unix = strtotime($UK) ;
date_default_timezone_set(`Asia/Singapore`);
$LOCAL = date(`Y-m-d H:i:s`) ;
$LOCAL_unix = strtotime($UK, $LOCAL ) ;

print "UK: $UK
$UK_unix

Your Location
$LOCAL $LOCAL_unix" ;
$DIFF = $UK_unix - $LOCAL_unix ;
$DIFF_HOURS = $DIFF / (60 * 60) ;
print "Difference: $DIFF seconds, $DIFF_HOURS hours" ;

$matchday_fix = "2025-01-21" ;
$KICKOFF = "15:00" ;

$matchday = my_date ( $matchday_fix, "" ) ;

print " Next Match: $matchday $KICKOFF

" ;
print "... waiting " ;


The Countdown Javscript code

This is set to run very second in the users browser.
- get the Date in unixtimestamp.
- then work out the timezone offset and work out how many seconds difference.
- work out time distance (gap) between two events and the Event Time.
- display Countdown time or Started

< script>
// Set the date we`re counting down to
var countDownDate = new Date("").getTime();
// Update the count down every 1 second
var x = setInterval( countdown, 1000 ) ;
function countdown( )
{
name = "countdown_fix" ;
var now = new Date().getTime() ;
var now_date = new Date() ;
now_offset = Math.abs( now_date.getTimezoneOffset() * 60 * 1000 ) ;
var distance = countDownDate - now + now_offset ;
// Time calculations for days, hours, minutes and seconds
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
if (distance > 0)
document.getElementById(name).innerHTML = " KO: " + days + "d " + hours + "h " + minutes + "m " + seconds + "s ";
else
{
clearInterval(x);
document.getElementById("countdown_fix").innerHTML = " Started /past";
}
}
< /script>