How to cound with PHP and print resulsts live according to current time

Discussion in 'JavaScript' started by tautvys92, Dec 2, 2009.

  1. #1
    How to cound with PHP and print resulsts live according to current time?
    For example, here is my PHP code. I get number, changing every second.

    $now=date("Y-m-d H:i:s");
    $now_seconds = strtotime( "$now" );
    $time_ago_seconds = strtotime( "$my_time_from_database" );
    $diference_in_seconds = $now_seconds - $time_ago_seconds; 
    Code (markup):
    How is it posible to see real time counting results without refreshing page? Should I use mootools, prototype or something else?
    P.S. I've no knowledge in JS or AJAX.
     
    tautvys92, Dec 2, 2009 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    Try follow:

    
    <?php
    $now=date("Y-m-d H:i:s");
    $now_seconds = strtotime( "$now" );
    $time_ago_seconds = strtotime( "$my_time_from_database" );
    $diference_in_seconds = $now_seconds - $time_ago_seconds;
    ?>
    <html>
    <head>
    <script type="text/javascript">
    var diference_in_seconds = <?php echo($diference_in_seconds); ?>;
    var t;
    
    function timedCount(){
        document.getElementById('seconds').innerHTML = diference_in_seconds;
        diference_in_seconds = diference_in_seconds + 1;
        t = setTimeout("timedCount()",1000);
    }
    </script>
    </head>
    
    <body>
    <div id="seconds"></div>
    </body>
    <script>timedCount();</script>
    </html>
    
    Code (markup):
     
    s_ruben, Dec 3, 2009 IP
  3. tautvys92

    tautvys92 Peon

    Messages:
    246
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    OMG... Thank you so much! It's working perfectly. After a lot of asking this thing in various forums at least I get it working. Now it's pretty easy to edit main features and make that I need. Thank you again.
     
    tautvys92, Dec 3, 2009 IP