PHP dynamic counter?
I found this script online and I need to modify it to fit my needs and I've t开发者_开发百科ried some stuff, but I'm in over my head.
The script I found is located here: http://www.daniweb.com/web-development/php/threads/68355
I need a similar script that basically will start with the number 32000 (based on let's say midnight on August 22nd), and then go up by 5 every 10 minutes forever.
Can anyone help me using that example? Or point me to an existing example elsewhere?
Thanks so much! I've pasted the code from my included link below:
<?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 id="carb">Calculating...</div>
Here's the PHP to calculate how many "points" between Aug 22 and now:
$start = mktime(0, 0, 0, 8, 22, 2011); // Aug 22, 2011
$diff = time() - $start; // seconds between start and now
$extra = 5 * floor($diff / 600);
$result = 32000 + $extra;
Basically something like this:
$now = time();
$start = mktime(0, 0, 0, 1, 24, 2007);
$init =((($now - $start)) + 130000);
?>
<script type="text/javascript">
var init = <?php print($init); ?>;
var val
function incs()
{
//Increase by random number (4-6)
init = init + Math.floor( (Math.random()*4)+3);
document.getElementById("counter").innerHTML=init;
//Create a random timer to make it not so obvious.
var random_timer = Math.floor( (Math.random()*500)+2000);
setTimeout('incs()',random_timer );
}
</script>
</head>
<body onload="">
<div id="counter">Calculating...</div>
</body>
I had some difficulty grasping the questions at first, so i hope this is what you were after. Basically make a starttime, with php, then use javascript and random numbers to increase the number as long as the page is up. replace the Math.floor() with static number if you want.
精彩评论