PHP动态计数器?

时间:2011-08-22 09:31:03

标签: php dynamic counter

我发现这个脚本在线,我需要修改它以满足我的需要,我已经尝试了一些东西,但我已经在我的头上了。

我找到的脚本位于:http://www.daniweb.com/web-development/php/threads/68355

我需要一个类似的脚本,基本上从数字32000开始(基于让我们说8月22日的午夜),然后永远每10分钟上升5点。

任何人都可以帮我使用这个例子吗?或者指出我在其他地方的现有例子?

非常感谢!我已粘贴以下链接中的代码:

<?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>

2 个答案:

答案 0 :(得分:1)

这是用于计算8月22日到现在之间有多少“点数”的PHP:

$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;

答案 1 :(得分:0)

基本上是这样的:

$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>

我一开始很难理解这些问题,所以我希望这就是你所追求的。 基本上使用php创建一个starttime,然后使用javascript和随机数来增加数量,只要页面启动。 如果需要,用静态数字替换Math.floor()。