I have built a voucher website, where each voucher has a countdown timer. When you view a voucher the countdown works fine, however... I also have a summary page with multiple vouchers (using a repeat region on the recordset)... when I apply the script to each of the vouchers the script doesn t work.
HMTL :
<h3 class="remaining"><?php echo $row_rs_dealItem[ dateend ]; ?> remaining</h3>
JAVASCRIPT外部文件:
$(document).ready(function(){
$( .remaining ).each(function(){
var expiry_date = Date.createFromMysql($(this).html());
var current_date = new Date();
console.log(expiry_date.getTime() );
console.log(current_date.getTime());
if (expiry_date.getTime() > current_date.getTime()) {
var time_diff = Math.floor((expiry_date.getTime() - current_date.getTime()) / (1000*60*60));
console.log(expiry_date.getTime() - current_date.getTime());
console.log(time_diff);
days_diff = Math.floor(time_diff / 24);
hours_diff = time_diff % 24;
$(this).html(days_diff + days + hours_diff + hours );
}
else{
$(this).html( expired );
}
});
});
Date.createFromMysql = function(mysql_string)
{
if(typeof mysql_string === string )
{
var t = mysql_string.split(/[- :]/);
//when t[3], t[4] and t[5] are missing they defaults to zero
return new Date(t[0], t[1] - 1, t[2], t[3] || 0, t[4] || 0, t[5] || 0);
}
return null;
}
JAVASCRIPT 内线:
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="scripts/script.js"></script>
<script type="text/javascript">
var reference = <?php echo $row_rs_dealItem[ reference ]; ?> ;
var today = Date.createFromMysql( <?php echo date( Y-m-d h:i:s ); ?> );
var contractstarts = <?php echo ($row_rs_dealItem[ datestart ] == 0000-00-00 ) ? "" : Date.createFromMysql(" . $row_rs_dealItem[ datestart ] . ") ; ?>;
var contractexpires = <?php echo ($row_rs_dealItem[ dateend ] == 0000-00-00 ) ? "" : Date.createFromMysql(" . $row_rs_dealItem[ dateend ] . ") ; ?>;
</script>
我希望这个数据足够你用
感谢 谢谢