1

right now i am displaying same countdown timer by default in all product view pages using below code. but i need different countdown timers in every product pages according to value we set for the attribute.

using custom module , we set Date & time for the attribute : ecomdev_expiry_date

as May 11 2016 - 12 : 15: 15

enter image description here

if current date & time is May 10 2016 - 12 : 15: 15 , than in view page i want to display

Time left to Buy : 24: 00: 00 and it should keep on updating as 23 : 59: 59 , 23: 59, 58 so on as like :

enter image description here

phtml

<?php 

$end_datetime='2016/06/10 12:15:15';

$date =  date( "Y/m/d H:i:s",strtotime($end_datetime));   
$endstr= date(strtotime($date));

$nowstr = Mage::getModel('core/date')->timestamp(time());
$now =  date('Y/m/d H:i:s', $nowstr); ?>

<span id="countdown" class"countdown">
<script>showtiming('<?php echo $date?>','<?php echo $now ?>')</script>
</span>

script

<script>

 function showtiming(dt,no)
{

 var end = new Date(dt);
 var now_date= new Date(no);

    var _second = 1000;
    var _minute = _second * 60;
    var _hour = _minute * 60;
    var _day = _hour * 24;
      flag_time = true;
    timer = '';
    setInterval(function(){showRemaining();},1000);

    function showRemaining() 
    {
        var date = no;
                var now = new Date(date);
                var de= now.getTime();

                if ( flag_time ) {
                timer = de;
                }
                var d = new Date(timer);
                currentYear = d.getFullYear();
                month = d.getMonth()+1;

                var currentDate = d.getDate();
                currentDate = currentDate < 10 ? '0'+currentDate : currentDate;

                var hours = d.getHours();
                var minutes = d.getMinutes();
                var seconds = d.getSeconds();

                minutes = minutes < 10 ? '0'+minutes : minutes;
                seconds = seconds < 10 ? '0'+seconds : seconds;
                var strTime = hours + ':' + minutes+ ':' + seconds; 
                timer = timer + 1000;
               var now_time = currentYear+'/' + month+'/' + currentDate + ' ' + strTime ;


        var now = new Date(now_time);

        var distance = end - now;

        if (distance < 0) {
             if (distance>-3) 
                {
                    location.reload(true); 
                    return;
                };

            clearInterval(timer);
            document.getElementById('countdown').innerHTML = 'Completed';


            return;
        }
        var days = Math.floor(distance / _day);
        var hours = Math.floor((distance % _day) / _hour);
        var minutes = Math.floor((distance % _hour) / _minute);
        var seconds = Math.floor((distance % _minute) / _second);
       if (days <10)
        {days = '0' + days;}
    if (hours <10)
        {hours = '0' + hours;}
    if (minutes < 10)
       { minutes = '0' + minutes;}
    if (seconds < 10)
        {seconds = '0' + seconds; }
    if(days==00)
    {
                    dytext = '';   

    }
    else if(days==1)
    {
        dytext = '<span class="countdown-text">'+ days +' Day  </span>';   
    }
    else
    {
         dytext = '<span class="countdown-text">'+ days +' Days  </span>';    
    }
    if(hours==00) {
        hrtext='';
    }
    else {
        hrtext = '<span class="countdown-text">'+ hours + '</span>'+ ":";
    }
    if(minutes==00)
    {
        mintext='';
    }else {
              mintext = '<span class="countdown-text">'+ minutes +'</span>'+ ":";

    }
        sectext = '<span class="countdown-text">'+ seconds +'</span>';
        document.getElementById('countdown').innerHTML = dytext + hrtext +  mintext + sectext;
       flag_time = false;
    }


}

</script>

<script> showtiming('<?php echo $date?>','<?php echo $now ?>') </script>
12
  • $end_datetime=$_product->geteComdevExpiryDate(); check this showing the correct values? Commented May 10, 2016 at 12:44
  • @QaisarSatti now it showing "completed" text for all pages : link1 link2 . please click on link 2nd time. Commented May 10, 2016 at 12:54
  • echo $_product->geteComdevExpiryDate(); and tell me what the output of that Commented May 10, 2016 at 12:55
  • its not displaying any value Commented May 10, 2016 at 12:57
  • i added the code above : <?php $end_datetime=$_product->geteComdevExpiryDate(); Commented May 10, 2016 at 12:58

1 Answer 1

1

remove this line in js document.getElementById('countdown').innerHTML = 'Completed';

and add date like <?php $end_datetime=$_product->getEcomdevExpiryDate(); ?>

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.