2011-08-22 40 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()。