hi i was set the task to increment a number by x every x seconds and i tried to find some code off the net, but found the subject covered very sparsley so for whoevers benefit ill post the code here hope it helps :)

<html>
<head>
<style type="text/css">
div.cont {
position: relative;
background-image: url(counter.gif);
width:160px;
height:110px;
vertical-align:text-bottom;
}
div.cont div.ans
{
position: absolute;
bottom: 0px;
margin-bottom:15px;
margin-left:7px;
color:black;
font-family: Verdana, Tahoma, Sans-Serif;
font-size: 15pt;
line-height: normal;
}

</style>

<?php

$now = time();
$start = mktime(0, 0, 0, 1, 24, 2007);
$carbonsaving =((($now - $start) * 0.0058774) + 130000);
$format = round($carbonsaving, 2);
// in this example
// $now = a unix timestamp of this very second
// $start is the date that you want the counter to start from sent over //as a unix timestamp
// $carbonsaving is the calculation that you want to perform to get //your base figure
// i.e. total saving = ((date now - start date)* growth rate) + base rate
// this gives us the starting saving all that needs to be done is increment it with javascript
?>

<script type="text/javascript">
// we need to import our server side variable into javascript to let it increment live

var car = <?php  print($format);  ?>;
var rou

function incs()
{
car = car + 0.01;
rou = Math.round(car*100)/100
document.getElementById("carb").innerHTML=rou;
}
// what function incs does is take car and adds 0.01 to it
//rou rounds the figure to 2 dp
//the document.getElementById("carb") can refer to a <p> tag //<span> or whatever and just says with .innerHTML=rou; that the //value between the  results of rou
//hope this helps
//Nicholas King
//ecotricity
</script>
</head>
<!-- body onload setInterval tells the page to load our javascript function and repeat it by every x microseconds, so this repeats every 2 seconds //-->
<body onload="setInterval('incs()', 2000)">
<div class="cont">
<div class="ans">
<span id="carb">Calculating...</span>
</div>
</div>
</body>
</html>

i hope this is of some use to someone

Thanks

Nicholas

Recommended Answers

All 9 Replies

Thanks for sharing :)

sorry, didnt realise there was one will do that,

thanks

Thank you this helped me.. I needed a simple counter that would automatically update once a week. It didn't have to be live so the javascript part of your code wasn't needed.

This works:

<?php
//Calculate value.. increment 100 weekly
$now = time();        					 //current time in secs
$start = mktime(0, 0, 0, 4, 30, 2010);   // start count from this date in secs
$startvalue = 100;						//start  value
$week = (($now - $start) / 604800);     //Week = 604800secs
$week = round($week);
$value = (($week * 100) + $startvalue);
?>

Then just include $value in your page.

Found an error in the code above. The

$week = round($week);

is wrong. It would round up half way through the week.
Change it to:

$week = intval($week);

Hi. This is almost exactly what I'm looking for. My client needs a counter that increases at the moment by 250 a day, but will increase by less or more in the future depending on the number of their employees. I want to try the above suggested solutions, but am not sure how to implement this in Joomla, am not very clued up with Coding. Can anyone help?

Member Avatar for Stefan_5

Hi guys very nice script, but i can't get it to work for my needs :(

I have to start with 5.190.000 and add +1 every 20 seconds, any help?

Thank you!

Hello guys, please I am building a laravel app where users make payment online and the amount is stored on the database. I want to be able to call the amount from the database so it displays on the specified user that made the payment.
Secondly, I want the amount to multiply by a certain percentage every 24 hours.
Please any help or assist on how to go around this... I'm new to php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.