如何显示每个产品的计时器

时间:2014-10-27 08:41:21

标签: javascript php jquery

我需要为每个产品显示计时器... 当我把我的代码放在循环中它只给一个产品定时器.... 我需要在我的产品页面中显示每个产品的计时器...我正在创建当天的交易... 我做了一些工作......它仅仅工作一种产品...... 我需要知道,这是给每个产品定时器的最好方法...... 目前我在上一个产品中得到计时器......

我的代码如下:

<div class="row">
    <!-- <h3>New Products</h3> --> 
    <?php if ($this->getProducts()->getSize() > '0') { ?>

        <?php foreach ($this->getProducts() as $_Product) { ?> 

            <?php //echo $timespan = strtotime($_Product['special_to_date']) - strtotime($_Product['special_from_date']);exit; ?>
            <div style="float: left; margin-left: 10px;">
                <div><a href="<?php echo $_Product->getProductUrl(); ?>"><img src="<?php echo $this->helper('catalog/image')->init($_Product, 'small_image')->resize(145, 200) ?>" alt="<?php echo $_Product->getName(); ?>"></a></div>
                <div align="center"><h4><?php echo $_Product->getName(); ?></h4></div>
                <div align="center"><?php echo Mage::helper('core')->formatPrice($_Product->getPrice()); ?></div>
                   <?php // echo "<pre>"; print_r($_Product->toArray());exit; ?>
                <div id="timeremaining<?php echo $_Product['sku'] ?>"></div>
            </div>

            <script>
                var target_date = new Date('<?php echo $_Product['special_to_date'] ?>').getTime();
         //alert(target_date);
                var localMinDiff = new Date().getTimezoneOffset() * 60000;  // get the difference between local time and GMT >> in mins >> mins to milliseconds conversion
                target_date = target_date - localMinDiff;
                var days, hours, minutes, seconds;
                var countdown = document.getElementById("timeremaining<?php echo $_Product['sku'] ?>");

                var countdownTimer = setInterval(function() {
                    var current_date = new Date().getTime();
                    var seconds_left = (target_date - current_date) / 1000;

                    days = parseInt(seconds_left / 86400);
                    seconds_left = seconds_left % 86400;

                    hours = parseInt(seconds_left / 3600);
                    seconds_left = seconds_left % 3600;
                    minutes = parseInt(seconds_left / 60);
                    seconds = parseInt(seconds_left % 60);

                    if (days <= 0 && hours <= 0 && minutes <= 0 && seconds <= 0)
                    {
                        countdown.innerHTML = '';
                        clearInterval(countdownTimer);
                    }
                    else
                    {
                        if (days > 0)
                        {
                            days = days + 'Days,';
                        }
                        else
                        {
                            days = '';
                        }
                        countdown.innerHTML = '( ' + days + checkTime(hours) + ':' + checkTime(minutes) + ':' + checkTime(seconds) + ' remaining)';
                    }
                }, 1000);

                function checkTime(i) {
                    if (i < 10) {
                        i = '0' + i
                    }
                    ;
                    return i;
                }

            </script>

        <?php } ?>
    <?php } ?>
</div>

1 个答案:

答案 0 :(得分:1)

要计算日期差异,您可以将日期字符串转换为时间戳(PHP <5.3):

$timespan = strtotime($endDate) - strtotime($startDate);
然后

$timespan将保持给定的两个日期之间的秒数,您可以进一步处理该值。

对于PHP&gt; = 5.3,您可以使用DateTime对象:

$start = new DateTime($startDate);
$end = new DateTime($endDate);
$interval = $end->diff($start);
echo $interval->format("%d days, %h hours, %i minutes, %s seconds");