April 24, 200917 yr Hi all. I have found this rather excellent countdown script, which uses a server side clock to do it's countdown. The script works just fine, and I have spent a while sucessfully integrating it into my CMS where I can give an item an expiry date and the count auto-inserts. Brilliant... Except that it won't do multiple calls on the same page. the result of the last call overwrites the first, and subsequent results do not display... I have done C++ coding many years ago so understand the principles, but am having real trouble decyphering the code as am pretty much a beginner at all of this. Could anybody help me alter this function to be able to call multiple scripts of the same function on one page? I have attached the php function, and a test html for anybody who might be able to help! What I really want to do is give each call a unique (random) id, which will then only pick up it's own results. Am I on the right track? Thanks. countdown.php index.html
July 3, 200917 yr when you call the php page, the php scriot create a js function called XXX(). if you call 3 times the same script, the in your final HTML you will have 3 times the js function XXX(). your browser will not know witch of the tree XXX() functions need to be precessed. to avoid this you need to call your functions each time in a different way: in coutdown.php <?php // we will be sending Javascript codes, remember... header('Content-Type: text/javascript'); //set script number in page $scriptNum = $_GET['scriptNum']; // select the timezone for your countdown $timezone = trim($_GET['timezone']); putenv("TZ=$timezone"); // Counting down to New Year's on 2020 $countdown_to = trim($_GET['countto']); // 24-Hour Format: YYYY-MM-DD HH:MM:SS" // Getting the current time $count_from = date("Y-m-d H:i:s"); // current time -- NO NEED TO CHANGE // Date difference function. Will be using below function datediff($interval, $datefrom, $dateto, $using_timestamps = false) { /* $interval can be: yyyy - Number of full years q - Number of full quarters m - Number of full months y - Difference between day numbers (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".) d - Number of full days w - Number of full weekdays ww - Number of full weeks h - Number of full hours n - Number of full minutes s - Number of full seconds (default) */ if (!$using_timestamps) { $datefrom = strtotime($datefrom, 0); $dateto = strtotime($dateto, 0); } $difference = $dateto - $datefrom; // Difference in seconds switch($interval) { case 'yyyy': // Number of full years $years_difference = floor($difference / 31536000); if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) { $years_difference--; } if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) { $years_difference++; } $datediff = $years_difference; break; case "q": // Number of full quarters $quarters_difference = floor($difference / 8035200); while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) { $months_difference++; } $quarters_difference--; $datediff = $quarters_difference; break; case "m": // Number of full months $months_difference = floor($difference / 2678400); while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) { $months_difference++; } $months_difference--; $datediff = $months_difference; break; case 'y': // Difference between day numbers $datediff = date("z", $dateto) - date("z", $datefrom); break; case "d": // Number of full days $datediff = floor($difference / 86400); break; case "w": // Number of full weekdays $days_difference = floor($difference / 86400); $weeks_difference = floor($days_difference / 7); // Complete weeks $first_day = date("w", $datefrom); $days_remainder = floor($days_difference % 7); $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder? if ($odd_days > 7) { // Sunday $days_remainder--; } if ($odd_days > 6) { // Saturday $days_remainder--; } $datediff = ($weeks_difference * 5) + $days_remainder; break; case "ww": // Number of full weeks $datediff = floor($difference / 604800); break; case "h": // Number of full hours $datediff = floor($difference / 3600); break; case "n": // Number of full minutes $datediff = floor($difference / 60); break; default: // Number of full seconds (default) $datediff = $difference; break; } return $datediff; } // getting Date difference in SECONDS $diff = datediff("s", $count_from, $countdown_to); ?> // Here’s where the Javascript starts countdown = <?=$diff?>; // Converting date difference from seconds to actual time function convert_to_time<?=$scriptNum?>(secs) { secs = parseInt(secs); hh = secs / 3600; hh = parseInt(hh); mmt = secs - (hh * 3600); mm = mmt / 60; mm = parseInt(mm); ss = mmt - (mm * 60); if (hh > 23) { dd = hh / 24; dd = parseInt(dd); hh = hh - (dd * 24); } else { dd = 0; } if (ss < 10) { ss = "0"+ss; } if (mm < 10) { mm = "0"+mm; } if (hh < 10) { hh = "0"+hh; } if (dd == 0) { return ("Time left: only " +hh+"hrs "+mm+"mins "+ss+ "secs to go!" ); } else { if (dd > 1) { return ("Time left: " +dd+ " days" ); } else { return ("Time left: " +dd+ "day" ); } } } // Our function that will do the actual countdown function do_cd<?=$scriptNum?>() { if (countdown < 0) { <?php if(strtolower(trim($_GET['do'])) == 'r' ) { ?> // redirect web page document.location.href = "<?=$_GET['data']?>"; <?php } ?> <?php if(strtolower(trim($_GET['do'])) == 't' ) { ?> // change text document.getElementById('cd').innerHTML = "<?=$_GET['data']?>"; <?php } ?> } else { document.getElementById('cd').innerHTML = convert_to_time<?=$scriptNum?>(countdown); setTimeout('do_cd()', 1000); } countdown = countdown - 1; } document.write("<div id='cd'></div>\n"); do_cd<?=$scriptNum?>(); <? exit(); ?> and in index.html <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <style type="text/css"> <!-- #cd { margin: auto; height: 50px; width: 450px; font-family: "Courier New", Courier, mono; font-size: 24pt; color: #000; text-align: center; font-weight: bold; background-image: url(back.jpg); vertical-align: middle; } --> </style> </head> <body> <h1 align="center">Counting Down to New Year's 2020</h1> <p align="center">According to the time at Kuala Lumpur, Malaysia. </p> <script language="JavaScript" SRC="countdown.php?scriptNum=1&timezone=Europe/London&countto=2009-04-24 00:00:00&do=t&data=WAWASAN 2020"></SCRIPT> <p> </p> <h1 align="center">Counting Down to New Year's 2020</h1> <p align="center">According to the time at Kuala Lumpur, Malaysia. </p> <script language="JavaScript" SRC="countdown.php?scriptNum=2&timezone=Europe/London&countto=2009-04-26 00:00:00&do=t&data=WAWASAN 2020"></SCRIPT> <p> </p> <h1 align="center">Counting Down to New Year's 2020</h1> <p align="center">According to the time at Kuala Lumpur, Malaysia. </p> <script language="JavaScript" SRC="countdown.php?scriptNum=3&timezone=Europe/London&countto=2009-04-28 00:00:00&do=t&data=WAWASAN 2020"></SCRIPT> <p> </p> </body> </html>
July 3, 200917 yr sorry: I forgot to insert some more $scriptNum: in the countdown.php: <?php // we will be sending Javascript codes, remember... header('Content-Type: text/javascript'); //set script number in page $scriptNum = $_GET['scriptNum']; // select the timezone for your countdown $timezone = trim($_GET['timezone']); putenv("TZ=$timezone"); // Counting down to New Year's on 2020 $countdown_to = trim($_GET['countto']); // 24-Hour Format: YYYY-MM-DD HH:MM:SS" // Getting the current time $count_from = date("Y-m-d H:i:s"); // current time -- NO NEED TO CHANGE // Date difference function. Will be using below function datediff($interval, $datefrom, $dateto, $using_timestamps = false) { /* $interval can be: yyyy - Number of full years q - Number of full quarters m - Number of full months y - Difference between day numbers (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".) d - Number of full days w - Number of full weekdays ww - Number of full weeks h - Number of full hours n - Number of full minutes s - Number of full seconds (default) */ if (!$using_timestamps) { $datefrom = strtotime($datefrom, 0); $dateto = strtotime($dateto, 0); } $difference = $dateto - $datefrom; // Difference in seconds switch($interval) { case 'yyyy': // Number of full years $years_difference = floor($difference / 31536000); if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) { $years_difference--; } if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) { $years_difference++; } $datediff = $years_difference; break; case "q": // Number of full quarters $quarters_difference = floor($difference / 8035200); while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) { $months_difference++; } $quarters_difference--; $datediff = $quarters_difference; break; case "m": // Number of full months $months_difference = floor($difference / 2678400); while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) { $months_difference++; } $months_difference--; $datediff = $months_difference; break; case 'y': // Difference between day numbers $datediff = date("z", $dateto) - date("z", $datefrom); break; case "d": // Number of full days $datediff = floor($difference / 86400); break; case "w": // Number of full weekdays $days_difference = floor($difference / 86400); $weeks_difference = floor($days_difference / 7); // Complete weeks $first_day = date("w", $datefrom); $days_remainder = floor($days_difference % 7); $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder? if ($odd_days > 7) { // Sunday $days_remainder--; } if ($odd_days > 6) { // Saturday $days_remainder--; } $datediff = ($weeks_difference * 5) + $days_remainder; break; case "ww": // Number of full weeks $datediff = floor($difference / 604800); break; case "h": // Number of full hours $datediff = floor($difference / 3600); break; case "n": // Number of full minutes $datediff = floor($difference / 60); break; default: // Number of full seconds (default) $datediff = $difference; break; } return $datediff; } // getting Date difference in SECONDS $diff = datediff("s", $count_from, $countdown_to); ?> // Here’s where the Javascript starts countdown<?php echo $scriptNum; ?> = <?php echo $diff; ?>; // Converting date difference from seconds to actual time function convert_to_time<?php echo $scriptNum; ?>(secs) { secs = parseInt(secs); hh = secs / 3600; hh = parseInt(hh); mmt = secs - (hh * 3600); mm = mmt / 60; mm = parseInt(mm); ss = mmt - (mm * 60); if (hh > 23) { dd = hh / 24; dd = parseInt(dd); hh = hh - (dd * 24); } else { dd = 0; } if (ss < 10) { ss = "0"+ss; } if (mm < 10) { mm = "0"+mm; } if (hh < 10) { hh = "0"+hh; } if (dd == 0) { return ("Time left: only " +hh+"hrs "+mm+"mins "+ss+ "secs to go!" ); } else { if (dd > 1) { return ("Time left: " +dd+ " days" ); } else { return ("Time left: " +dd+ "day" ); } } } // Our function that will do the actual countdown function do_cd<?php echo $scriptNum; ?>() { if (countdown<?php echo $scriptNum; ?> < 0) { <?php if(strtolower(trim($_GET['do'])) == 'r' ) { ?> // redirect web page document.location.href = "<?php echo $_GET['data']; ?>"; <?php } ?> <?php if(strtolower(trim($_GET['do'])) == 't' ) { ?> // change text document.getElementById('cd<?php echo $scriptNum; ?>').innerHTML = "<?php echo $_GET['data']; ?>"; <?php } ?> } else { document.getElementById('cd<?php echo $scriptNum; ?>').innerHTML = convert_to_time<?php echo $scriptNum; ?>(countdown<?php echo $scriptNum; ?>); setTimeout('do_cd<?php echo $scriptNum; ?>()', 1000); } countdown<?php echo $scriptNum; ?> = countdown<?php echo $scriptNum; ?> - 1; } document.write("<div id='cd<?php echo $scriptNum; ?>'></div>\n"); do_cd<?php echo $scriptNum; ?>(); <?php exit(); ?>
Create an account or sign in to comment