2011-08-22 81 views
-3

我在网上发现了这个剧本,我需要修改它适合我的需要,我已经尝试了一些东西,但我在我头上要启用。PHP动态计数器?

我发现该脚本位于:http://www.daniweb.com/web-development/php/threads/68355

我需要一个类似的脚本,基本上将与数32000开始(基于假设8月22日午夜),然后通过5每10分钟上升永远。

谁能帮助我使用的例子吗?或者将我指向其他地方的现有示例?

非常感谢!我从我下面的包含链接粘贴代码:

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

究竟你需要的是什么?如果我们知道一些背景,我们可能会更好地帮助您。 – Rijk

+0

我试图在一个以基数开始的网站上放置一个计数器,然后继续往上走。 例如:这个很多人都在Facebook上 - 345001619,它通过x个上升每隔x分钟 – amg21

回答

1

这里的PHP 8月22日到现在之间来计算有多少“点”:

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

感谢Neokio,您的文章使我认识东西..其实我并不需要一个“活柜台”本身,只是一个PHP的数学问题,可以计算起始值(32000)+((时间() - $开始)/ X)或类似的东西? 更具体,我需要开始号32000,并请有5每10分钟增加。公式的开始日期方面起作用,因此数字一致地攀升并永远不会以32000开头 – amg21

+0

以上应该完全是这样做的 – neokio

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,然后使用JavaScript和随机数字来增加数量,只要页面是。 如果需要,用静态数字替换Math.floor()。