View Full Version : Count Up Script?

05-25-2012, 10:21 PM
Hey :( I searched a lot on Google and many other places but couldn't find what i want... It's simple..

I need a count up script for football that starts counting on a certain date i add... after it starts counting from 0' mins until 45' after that i want it to show a word "Half Time"...
if possible in the same script after 15 mins the script start counting from 45' to 90' after it reach minute 90 i want it to write "Finished" and stop..
but please i want it to keep counting even if i refresh the page not start over or something like that..
if making the script start counting again from 45 to 90 after 15 mins is impossible to do it then its ok with a script to count from 0 to 45 then show "HalfTime" and a script to count from 45 to 90 and show "Finished"

answers and help are much appreciated really!!!!! i found tons of coupt up scripts but couldn't be able to get it in the way i want coz of my lack in Javascript :(

Thanks in Advance!!!

05-26-2012, 01:11 AM
Why do you say both PHP and Javascript? Javascript can interact with the user (and count by itself), but PHP operates only on the server, so it's for things like interacting with a database.

Is this for a computer game? If so, it can work. But if this is for a real football game, the clock won't be accurate because of TV breaks for commercials, time outs, and other pauses in the game.

This is certainly possible, but some of the details need to be worked out.

05-26-2012, 04:03 AM
Here's my effort:

Javascript (timer.js):

// Class: Timer
var Timer = function (callback) {
// Property: Frequency of elapse event of the timer in milliseconds
this.Interval = 1000;

// Property: Whether the timer is enable or not
this.Enable = new Boolean(false);

// Event: Timer tick
this.Tick = callback;

// Member variable: Hold interval id of the timer
var timerId = 0;

// Member variable: Hold instance of this class
var thisObject;

// Function: Start the timer
this.Start = function () {
this.Enable = new Boolean(true);

thisObject = this;
if (thisObject.Enable) {
thisObject.timerId = setInterval(
function () {
}, thisObject.Interval);

// Function: Stops the timer
this.Stop = function () {
thisObject.Enable = new Boolean(false);


// Namespace: Match rules and timings
var Match = {

Timers: {
FirstHalf: new Timer(TimerTick),
HalfTime: new Timer(TimerTick),
SecondHalf: new Timer(TimerTick),
TickCount: -1

Strings: {
FirstHalf: 'First Half',
HalfTime: 'Half Time',
SecondHalf: 'Second Half',
FullTime: 'Finished'

DisplayTime: function (t) {
var m = parseInt(t / 60);
var s = t % 60;
return (m < 10 ? '0' + m : m) + ":" + (s < 10 ? '0' + s : s);

// Function: Tick Event Handler (callback function)
function TimerTick(timer) {

// Document elements used.
var TimerP = document.getElementById('time');
var DisplayP = document.getElementById('display');

// During First Half
if (Match.Timers.FirstHalf.Enable == true) {
if (Match.Timers.TickCount == -1) { Match.Timers.TickCount = 0 }
if (Match.Timers.TickCount == 2700) {
Match.Timers.TickCount = -1;
} else {
TimerP.innerHTML = Match.DisplayTime(Match.Timers.TickCount);
DisplayP.innerHTML = Match.Strings.FirstHalf;

// During Half Time
else if (Match.Timers.HalfTime.Enable == true) {
if (Match.Timers.TickCount == -1) { Match.Timers.TickCount = 0 }
if (Match.Timers.TickCount == 900) {
Match.Timers.TickCount = -1;
} else {
TimerP.innerHTML = '45:00';
DisplayP.innerHTML = Match.Strings.HalfTime + ' (' + Match.DisplayTime(900 - Match.Timers.TickCount) + ')';

// During Second Half
else if (Match.Timers.SecondHalf.Enable == true) {
if (Match.Timers.TickCount == -1) { Match.Timers.TickCount = 2700 }
if (Match.Timers.TickCount == 5400) {
TimerP.innerHTML = '90:00';
DisplayP.innerHTML = Match.Strings.FullTime;
Match.Timers.TickCount = -1;
} else {
TimerP.innerHTML = Match.DisplayTime(Match.Timers.TickCount);
DisplayP.innerHTML = Match.Strings.SecondHalf;

function KickOff() {
var btn = document.getElementById('btnKickOff');
btn.setAttribute('style','display: none;');


<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<!-- Start Meta Block -->
<meta content="text/html; charset=iso-8859-1" http-equiv="content-type" />
<meta content="Simple Football Match Timer" name="description" />
<meta content="Simple, Football, Match, Timer" name="keywords" />
<meta content="all,index,follow" name="robots" />
<meta content="noodp" name="msnbot" />
<meta content="global" name="distribution" />
<!-- End Meta Block-->
<title>Simple Football Match Timer</title>
<script src="timer.js" type="text/javascript"></script>

<form id="pageForm" runat="server">
<p id="display">Waiting for kick off.</p>
<p id="time">00:00</p>
<input id="btnKickOff" type="button" value="Kick Off!" onclick="KickOff();" />

I think it has everything you asked for.

05-26-2012, 06:35 AM
@djr33 Sorry i wrote PHP by mistake!.. and thanks for explaining for me!.. Well it doesn't matter for me if its in PHP or JS

ApacheTech Did a great script.. Thanks mate!, but the problem is that i want it to keep counting even when i refresh page... i want users to be able to see what time the match is now during the match... and in fooball theres no commercials, just in during the Halftime which is 15 minutes
So the script is just great but how can i make it not start over with ever page refresh? Thanks.

05-26-2012, 12:55 PM
How would you factor in the injury time and stoppages?

Is it for live matches IRL?

05-26-2012, 09:13 PM
Oh, football = soccer, not football = American football. I see. That's easier, but as ApacheTech said, it's not just counting, right? Time outs, for example.

Using Javascript alone is probably the best approach here. However, you might want to use PHP to originally set the starting number for Javascript when the page loads, if you need it to be the same time when you refresh the page.

05-26-2012, 11:26 PM
Really, only yanks call american football, football. To most of the planet, football is what the yanks call soccer. Most of the rest of the world calls american football, "Rugby for wimps". :p

Don't get me wrong, football is even worse than american football. Never could stand it.

If you want to simulate a game, this is easier than tracking a live game.

We could throw in some random events that stop the clock every so often, like players crying because someone came within two feet of them, faked injuries, pathetic goal celebrations and the usual idiocy that goes on.

You wouldn't need the Timer class in that case though, you'd need a different form of timer that counts from a set time and uses a DateDiff as the Tick.

05-27-2012, 06:25 AM
@ApacheTech, yes you're right... i forgot about the additional time that gets added at the end of each half by the referee before the 15 mins break starts :/
I really appreciate your help its a great script!
but idk how could i make it start on a fixed date and keep counting no matter if i refresh the page

05-27-2012, 07:06 AM
You could use a cookie to remember the time, or you could use a real-life reference time (for example, 2pm on May 28) or you could store that real-life reference time in PHP and set the Javascript timer relative to that.

05-27-2012, 02:35 PM
The only PHP IDE I have available is Visual Studio 2010 with Phalanger installed, but it doesn't seem to be set up right. I couldn't even get DateTime.

You could set up a class in PHP with random events, timed events, stringlist and everything you need.

If you're simulating a match, you could have the fixtures list in a database:

team_id (Autonumber)
team_name (String)

fixture_id (Autonumber)
home_team_id (Lookup: tblTeams.team_id)
away_team_id (Lookup: tblTeams.team_id)
fixture_timestamp (DateTime)

Then you can load each fixture into a Fixture class as an array:


class Fixture {

// Private Members
private $m_hometeam;
private $m_awayteam;
private $m_kickofftime;

// Constructor
function __construct($_hometeam, $_awayteam, $_kickofftime) {
$this->m_hometeam = $_hometeam;
$this->m_awayteam = $_awayteam;
$this->m_kickofftime = $_kickofftime;

// Public Properties
public function HomeTeam() {
return $this->m_hometeam;

public function AwayTeam() {
return $this->m_awayteam;

public function KickOffTime() {
return date_format($this->m_kickofftime, 'Y-m-d H:i:s');



$sql = 'SELECT T.team_name, F.home_team_id, F.away_team_id, F.fixture_timestamp FROM tblFixtures AS F, tblTeams AS T ORDER BY F.fixture_timestamp';
$result = mysql_query($sql);

$FixturesList = array();

while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
array_push($FixturesList, new Fixture($row['home_team'], $row['away_team'], $row['fixture_timestamp']));


This is obviously only extremely basic but it'll give you a starting place.

05-27-2012, 09:48 PM
I Really appreciate your help!

ill show you where i wanna use this script...
That's my output page (http://leb-media.com/fouadlb/files/GermanySupporters/Scores/index.php)
that's how it looks in PHP (http://pastebin.com/FKVkGJPZ) it is connected to a XML (http://pastebin.com/ZE8FA8MY) file that gives it the info for the table rows...

ok so the script will be placed instead the word "Finished" when the match is live the countdown would show what time the match is now...

the JS u wrote for me is awesome and that's what im looking for (Except the problem of the added time for each half:P)

btw i got help for creating this script :P
I'm a beginner, im starting to learn PHP/Javascript... already finished HTML and CSS :)

I was told to use Database MySQL instead of XML but i have no idea how i can do that... how i can connect the PHP to the DB and how i can get the info from the DB to post it...(Explaining that i got nothing of what u wrote in the last post :P)

Well that's how learning starts :) taking examples getting help from experts and practicing... that's the best way to learn...


05-27-2012, 10:55 PM
It would be impossible to get an accurate "following" script for live matches, unless UEFA has an API set up for following matches, which I highly doubt; or a live Atom feed for each match.

If there is a live Atom Feed, you could pull the results and stats from that instead of creating a database or an XML file.

For the rest of it, I can't really be of too much help. I'm a .NET WebDev really, only a beginner with PHP and I write databases in Access, I have no idea about MySQL or any of the other options. Wouldn't even know where to start without the Access 'IDE'. I'm starting to learn the SQL query strings but it's still very new to me.

05-27-2012, 11:55 PM
I agree here-- your best strategy will be to look for an API. This is all technically possible, except for coordinating the script with real life. Unless you want to create a computer vision program that can watch the game and figure out how much time is remaining from the TV feed, you really can't do this abstractly with math. So find a useful API if it exists.

05-28-2012, 03:49 AM
This is football we're talking about... they're not going to have an API, lol. Most players couldn't even spell it and I highly doubt FIFA, UEFA or any of the other governing bodies will have thought about computer programmers when dishing out millions of pounds to school drop outs for being slightly better than average at kicking things.

05-28-2012, 04:17 AM
You're claiming that no one in the world who cares about football could build an API? Your posting style is sometimes very frustrating (and unproductive).

Obviously you won't be searching for the player's sites, or even probably the teams. But if you search around, you may find a fan site (or a sports website in general) that does have an API.

05-28-2012, 04:37 AM
My apologies. I didn't mean to cause offense. I've always been very cynical and jaded when it comes to football.

And my cynicism usually forces karma to turn on me, as with this.

So to make up for it, I bring you:


You could also strip the data you need from:


Or take it from an RSS feed from:


05-28-2012, 04:49 AM
I've always been very cynical and jaded when it comes to football. And that's fine, but it doesn't belong in posts here. We can assume the original poster does want to have a script that works and is about football, since that was the original question.

Yes, those two options look entirely reasonable. Of course it will be a different configuration than just a timer (probably also including a timer to fill in the details while waiting for an update from the API), but it should get the job done.