5

I am attempting to write a JS count up timer (that will just count up to infinity) based on a PHP variable. The variable unfortunately is uneditable (can do things to it once I get it, but can't change the form of what I get since it's coming from a database software writes into, and I can't change the software). All I get is seconds (how many seconds ago something user accesed, etc) and would like that to be displayed in a format of (X Days, X hours, X minutes, X seconds).

I already have a code for the timer and the PHP code too, however the question is what would be the best approach at the solution. Do I try to get the seconds into a date in PHP then let JS do all rest or do I change JS so that it accepts the seconds and then do the countdown. As well as this, how would I do it?

Code I have for the counter:

JS inside the document, passing in the variables so that PHP can change these easily:

<script type="text/javascript" language="JavaScript">
    TargetDate = "09/21/2012 5:00";
    CountActive = true;
</script>

and then (including all the counter code):

<script type="text/javascript"  language="JavaScript" src="js/time.js"></script>

And the .js file as mentioned above:

function calcage(secs, num1, num2) {
  s = ((Math.floor(secs/num1))%num2).toString();
  if (LeadingZero && s.length < 2)
    s = "0" + s;
  return  s;
}

function CountBack(secs) {
  if (secs < 0) {
    document.getElementById("cntdwn").innerHTML = FinishMessage;
    return;
  }
  DisplayStr = DisplayFormat.replace(/%%D%%/g, calcage(secs,86400,100000));
  DisplayStr = DisplayStr.replace(/%%H%%/g, calcage(secs,3600,24));
  DisplayStr = DisplayStr.replace(/%%M%%/g, calcage(secs,60,60));
  DisplayStr = DisplayStr.replace(/%%S%%/g, calcage(secs,1,60));

  document.getElementById("cntdwn").innerHTML = DisplayStr;
  if (CountActive)
    setTimeout("CountBack(" + (secs+CountStepper) + ")", SetTimeOutPeriod);
}

function putspan(backcolor, forecolor) {
 document.write("<span id='cntdwn'></span>");
}

if (typeof(BackColor)=="undefined")
  BackColor = "white";
if (typeof(ForeColor)=="undefined")
  ForeColor= "#2A8827";
if (typeof(TargetDate)=="undefined")
  TargetDate = "12/31/2020 5:00 AM";
if (typeof(DisplayFormat)=="undefined")
  DisplayFormat = "%%D%% days, %%H%% hours, %%M%% minutes, %%S%% seconds.";
if (typeof(CountActive)=="undefined")
  CountActive = true;
if (typeof(FinishMessage)=="undefined")
  FinishMessage = "no data";
if (typeof(CountStepper)!="number")
  CountStepper = +1;
if (typeof(LeadingZero)=="undefined")
  LeadingZero = true;


CountStepper = Math.ceil(CountStepper);
if (CountStepper == 0)
  CountActive = false;
var SetTimeOutPeriod = (Math.abs(CountStepper)-1)*1000 + 1000;
putspan(BackColor, ForeColor);
var dthen = new Date(TargetDate);
var dnow = new Date();
if(CountStepper>0)
  ddiff = new Date(dnow-dthen);
else
  ddiff = new Date(dthen-dnow);
gsecs = Math.floor(ddiff.valueOf()/1000);
CountBack(gsecs);
3
  • 1
    The more processing you can assign to the server side with PHP, the less stress the client browser has to endure with JavaScript. Commented Oct 18, 2012 at 13:30
  • 1
    @jdstankovsky The more processing is done in client's browser with javascript, the lower the load on your server. Commented Oct 18, 2012 at 13:51
  • OK, so it's better to do it in PHP. Anyone got a suggestion of how to get the php to tell you the date that was X seconds ago (when X is the variable that is returned)? Commented Oct 18, 2012 at 13:56

1 Answer 1

1

As advised in the comments, I used PHP. Got to it eventually, by:

Getting the value from DB and storing it within $uptime (in the form of seconds).

$uptime = (time() - $uptime);
$uptime = date("m/d/Y H:i:s",$uptime);

Then I changed it using the date function to suit the JS code and output it with the new $uptime variable:

<script type="text/javascript" language="JavaScript">
TargetDate = "<?php echo $uptime; ?>";
CountActive = true;
</script>
<script type="text/javascript"  language="JavaScript" src="js/time.js"></script>

This made sure the date is outputted in the correct format, suitable for JS.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.