1

Hi I am trying to add multiple countdown timer through javascript but some how, its not working , Can any one tell me what I am doing wrong, I am a new bee.

below is by code:

php while loop {

    <script>
    var timer;

    var days= <?php echo $datediff ; ?>;     

    var compareDate = new Date();
    compareDate.setDate(compareDate.getDate() + <?php echo $datediff  ?> ); //just for this demo today + 7 days

    timer = setInterval(function() {
        timeBetweenDates(compareDate);
    }, 1000);

    function timeBetweenDates(toDate) {
        var dateEntered = toDate;
        var now = new Date();
        var difference = dateEntered.getTime() - now.getTime();

        if (difference <= 0) {
            // Timer done
            clearInterval(timer);
        } else {
            var seconds = Math.floor(difference / 1000);
            var minutes = Math.floor(seconds / 60);
            var hours = Math.floor(minutes / 60);
            var days = Math.floor(hours / 24);

            hours %= 24;
            minutes %= 60;
            seconds %= 60;

            $(".days") .text(days);
            $(".hours").text(hours);
            $(".minutes").text(minutes);
            $(".seconds").text(seconds);
       }    
    }
    </script>
    <div class="timer">
        <span class="days"></span>days
        <span class="hours"></span>hours
        <span class="minutes"></span>minutes
        <span class="seconds"></span>seconds
    </div>

I am not able to get the timer work have changed the id to class.

4
  • Hey, I tried and it worked for me. Did you check the console on the Developer Tools of your browser for any error? Show the full code here and I'll try to help you to figure out where the issue is. Commented Dec 28, 2016 at 6:04
  • Iam trying to work it inside a while loop here i fetch product info . Commented Dec 28, 2016 at 6:12
  • hold on let me put full code Commented Dec 28, 2016 at 6:12
  • while ($row = mysql_fetch_assoc($consulta)) { ?> Commented Dec 28, 2016 at 6:15

1 Answer 1

1

If I got the idea correctly here you need to play with global and local variables here not to overwrite values of the global scope in every loop.

Check this code and let me know if it helps you out in some way.

<?php
$datediffs = [7, 8, 9, 10]; //let's say this is the array of results from the database
?>
<html>
<head>
    <script src="//code.jquery.com/jquery-3.1.1.min.js"></script>
</head>
<body>
<script>
    var timer = []
    var timestamp = new Date()

    function timeBetweenDates(toDate, key) {
        var now = new Date()
        var difference = toDate.getTime() - now.getTime()

        if ( difference <= 0 )
        {
            // Timer done
            clearInterval( timer[key] );
        }
        else
        {
            var seconds = Math.floor( difference / 1000 )
            var minutes = Math.floor( seconds / 60 )
            var hours = Math.floor( minutes / 60 )
            var days = Math.floor( hours / 24 )

            hours %= 24
            minutes %= 60
            seconds %= 60

            var $timer = $( '.timer-' + key )

            $timer.find( '.days' ).text( days )
            $timer.find( '.hours' ).text( hours )
            $timer.find( '.minutes' ).text( minutes )
            $timer.find( '.seconds' ).text( seconds )
        }
    }
</script>

<?php foreach ( $datediffs as $key => $datediff ) : ?>
    <script>
        timer.push( setInterval( function()
        {
            var compareDate = new Date()
            compareDate.setTime( timestamp.getTime() )
            compareDate.setDate( compareDate.getDate() + <?php echo $datediff  ?> )
            timeBetweenDates( compareDate, <?php echo $key; ?> )
        }, 1000) )
    </script>

    <div class="timer-<?php echo $key; ?>">
        <span class="days"></span> days
        <span class="hours"></span> hours
        <span class="minutes"></span> minutes
        <span class="seconds"></span> seconds
    </div>
<?php endforeach; ?>
</body>
</html>
Sign up to request clarification or add additional context in comments.

9 Comments

can you replace for each loop with mysql while loop so it would be easy for me to understand ?
while ($row = mysql_fetch_assoc($consulta)) { ?> $datediff= $_row['days' ] }
ok i have two dates stored in db 1. 2017-12-27 ( closing date ) 2 2017-12-26( time stamp) now how do is start with
To change foreach for while just change the line <?php foreach ( $datediffs as $key => $datediff ) : ?> to <?php while ($row = mysql_fetch_assoc($consulta)) : $datediff= $_row['days' ] ?> and the line <?php endforeach; ?> by <?php endwhile; ?>
Sorry, I don't get the idea with the two dates stored in the DB, rephrase your comment please.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.