0

I use wordpress and I have static number for a field which is taken from sql query.

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

I would like to increase that number everyday. For example when the page is loaded default number is 843 next day it should show 844 the day after it should show 845.

How can I do this? I prefer PHP but if it is possible also can use jquery.

Anthon
  • 69,918
  • 32
  • 186
  • 246
Johnny
  • 1,685
  • 6
  • 24
  • 42

3 Answers3

1

jQuery Answer

You will have to setup the starting date for it to increase daily. The idea is to get the date difference and add it to that counter.

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);
});

I haven't tested it. But this is an idea to get that done.

syanaputra
  • 590
  • 1
  • 5
  • 12
  • Thank you Stephanus for your answer. I used php as th3falc0n provided. It was easier for me. – Johnny Mar 21 '15 at 07:58
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>

Code taken from Finding the number of days between two dates

This way it will always show the difference from the starting date to now, in days.

Community
  • 1
  • 1
th3falc0n
  • 1,389
  • 1
  • 12
  • 33
0

For your case here are two ways achieve the goal.

  1. use 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. use 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;
     ?>   
    

Hope this can help you !

Cherry
  • 388
  • 1
  • 3
  • 13