Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用php每天自动增加一个静态数字_Php_Jquery_Counter - Fatal编程技术网

如何使用php每天自动增加一个静态数字

如何使用php每天自动增加一个静态数字,php,jquery,counter,Php,Jquery,Counter,我使用wordpress,我有一个从sql查询中获取的字段的静态编号 <p class="counter-number">843</p> 843 我想每天增加这个数字。例如,加载页面时,第二天的默认数字为843,第二天应显示844,第二天应显示845 我该怎么做?我更喜欢PHP,但如果可能的话,也可以使用jquery。jquery-Answer 您必须设置每天增加的开始日期。这样做的目的是获取日期差并将其添加到计数器中 HTML <p class="counte

我使用wordpress,我有一个从sql查询中获取的字段的静态编号

<p class="counter-number">843</p>

843

我想每天增加这个数字。例如,加载页面时,第二天的默认数字为843,第二天应显示844,第二天应显示845


我该怎么做?我更喜欢PHP,但如果可能的话,也可以使用jquery。

jquery-Answer

您必须设置每天增加的开始日期。这样做的目的是获取日期差并将其添加到计数器中

HTML

<p class="counter-number">843</p>
我还没有测试过。但这是一个实现这一目标的想法。


<?php
     $now = time();
     $your_date = strtotime("2010-01-01"); //Starting date
     $datediff = floor(($now - $your_date)/(60*60*24));
?>
<p class="counter-number"><?=$datediff?></p>

代码取自


这样,它将始终显示从开始日期到现在的差异,以天为单位。

对于您的情况,这里有两种实现目标的方法

  • 使用php

    //client site php_cnt.html
    <html>
    <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <title>visit_cnt</title>                                                                                                                                  
    </head>
    <script type="text/javascript"src="http://localhost/php_cnt.php">
    </script>
    </html>
    
    //server site php_cnt.php
     <?php    
     //Here is the some logic to process the visit count                                                                                                                                                 
     $visit_cnt = 11; // assume read it from mysql
     echo "document.write($visit_cnt);";
     ?>     
    
    //客户端站点php\u cnt.html
    参观
    //服务器站点php_cnt.php
    
  • 使用javascript/jquery

    //client site php_cnt.html
    $(function() {
      $.get('http://localhost/php_cnt.php',{r:Math.random()},function(cnt) {
       $('. counter-number').html(cnt);
      });
    }); 
    //the server site code
    <?php    
     //Here is the some logic to process the visit count                                                                                                                                                 
     $visit_cnt = 11; // assume read it from mysql
     echo visit_cnt;
     ?>   
    
    //客户端站点php\u cnt.html
    $(函数(){
    $.get('http://localhost/php_cnt.php“,{r:Math.random()},函数(cnt){
    $('.counter number').html(cnt);
    });
    }); 
    //服务器站点代码
    

  • 希望这能帮助你

    谢谢你的回答。我使用php作为th3falc0n提供。对我来说更容易。
    //client site php_cnt.html
    $(function() {
      $.get('http://localhost/php_cnt.php',{r:Math.random()},function(cnt) {
       $('. counter-number').html(cnt);
      });
    }); 
    //the server site code
    <?php    
     //Here is the some logic to process the visit count                                                                                                                                                 
     $visit_cnt = 11; // assume read it from mysql
     echo visit_cnt;
     ?>