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

时间:2015-03-21 00:25:31

标签: php jquery counter

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

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

我想每天增加这个数字。例如,当页面加载时,默认号码是843,它应该在显示845后的第二天显示844.

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

4 个答案:

答案 0 :(得分:1)

jQuery回答

您必须设置每天增加的开始日期。想法是获取日期差异并将其添加到该计数器。

<强> HTML

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

<强>的jQuery

jQuery(function() {
    // Get Starting Number
    var starting_number = parseInt(jQuery('.counter-number').text());

    // Create Day difference (because it increases by 1 each day)
    var preset_start_date = new Date("21/03/2015");
    var current_date = new Date();
    var timeDiff = Math.abs(current_date.getTime() - preset_start_date.getTime());
    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24)); 

    var final_counter = starting_number + diffDays;
    jQuery('.counter-number').text(final_counter);
});

我还没有测试过它。但这是完成这项工作的一个想法。

答案 1 :(得分:1)

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

代码来自Finding the number of days between two dates

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

答案 2 :(得分:0)

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

  1. 使用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);";
     ?>     
    
  2. 使用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;
     ?>   
    
  3. 希望这可以帮到你!

答案 3 :(得分:-1)

创建将每天更新该数字的脚本,并使用cron作业执行该脚本。看看cron工作如何运作。