Javascript不能使用php变量

时间:2016-08-02 09:12:47

标签: javascript php

我的php& javascript代码。

while($row = mysqli_fetch_array($doquery, MYSQLI_ASSOC)) {



            echo ("

            <!-- Inizio Item-->
                <script type='text/javascript'>

                var end = new Date('".$row['fine']."');


                var _second = 1000;
                var _minute = _second * 60;
                var _hour = _minute * 60;
                var _day = _hour * 24;
                var timer;

                function showRemaining() {
                    var now = new Date();
                    var distance = end - now;
                    if (distance < 0) {

                        clearInterval(timer);
                        document.getElementById('countdown".$row['id']."').innerHTML = 'EXPIRED!';

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

                document.getElementById('countdown".$row['id']."').innerHTML = days + ' Days ';
                document.getElementById('countdown".$row['id']."').innerHTML += hours + ' Hours ';
                document.getElementById('countdown".$row['id']."').innerHTML += minutes + ' Mins ';
                document.getElementById('countdown".$row['id']."').innerHTML += seconds + ' Secs';
                } 
                </script>
                <div id='countdown".$row['id']."'></div>
           ");
}

如果我使用此代码,我会倒计时:

NaN days NaN Hours Nan Mins NaN Secs

我检查了

的输出
$row['fine'];

并从javascript询问输出的正确形式。事实上,输出为08/01/2017 3:45 PM

事实上,如果我尝试将日期直接放在代码中,就像这样:

... 
var end = new Date('08/01/2016 3:45 PM ');
...

它显示了应该执行的计时器。

1 个答案:

答案 0 :(得分:0)

试试这个(我假设你只有1行数据)并告诉我结果: -

$time_data = array();
while($row = mysqli_fetch_array($doquery, MYSQLI_ASSOC)) {
$time_data['fine'][]=$row['fine'];
$time_data['id'][]=$row['id']
}


            <!-- Inizio Item-->
                <script type='text/javascript'>

                var end = new Date('<?php echo trim($time_data['fine'][0]); ?>');


                var _second = 1000;
                var _minute = _second * 60;
                var _hour = _minute * 60;
                var _day = _hour * 24;
                var timer;

                function showRemaining() {
                    var now = new Date();
                    var distance = end - now;
                    if (distance < 0) {

                        clearInterval(timer);
                        document.getElementById('countdown<?php echo $time_data['id'][0]; ?>').innerHTML = 'EXPIRED!';

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

                document.getElementById('countdown<?php echo $time_data['id'][0]; ?>').innerHTML = days + ' Days ';
                document.getElementById('countdown<?php echo $time_data['id'][0]; ?>').innerHTML += hours + ' Hours ';
                document.getElementById('countdown<?php echo $time_data['id'][0]; ?>').innerHTML += minutes + ' Mins ';
                document.getElementById('countdown<?php echo $time_data['id'][0]; ?>').innerHTML += seconds + ' Secs';
                } 
                <div id='countdown<?php echo $time_data['id'][0]; ?>'></div>