php倒计时脚本-每页多个倒计时

问题描述

| 我有以下PHP倒计时脚本,它可以倒计时到日期和时间。请参见以下示例:
    <SCRIPT language=\"JavaScript\" SRC=\"countdown.PHP?timezone=US/Pacific&countto=2011-06-25 00:00:00&do=t&data=Sorry,This Offer Has Expired.\"></SCRIPT>  
还有countdown.PHP
<?PHP
// we will be sending Javascript codes
header(\'Content-Type: text/javascript\'); 

// 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\",date(\"s\",date(\"n\",date(\"j\",date(\"Y\",$datefrom)+$years_difference) > $dateto) {
    $years_difference--;
  }
  if (mktime(date(\"H\",$dateto),$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)+($quarters_difference*3),$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)+($months_difference),$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);
?>

// Heres where the Javascript starts
countdown = <?=$diff?>;

// Converting date difference from seconds to actual time
function convert_to_time(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 (hh+\":\"+mm+\":\"+ss); } 
else {  
    if (dd > 1) { return (dd+\" days \"+hh+\":\"+mm+\":\"+ss); }
    else { return (dd+\" day \"+hh+\":\"+mm+\":\"+ss); }
}   
}

// Our function that will do the actual countdown
function do_cd()
{
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(countdown);
    setTimeout(\'do_cd()\',1000);
}   
countdown = countdown - 1;  
}

document.write(\"<div id=\'cd\'></div>\\n\");

do_cd();

<? exit(); ?>
有人可以告诉我如何修改此脚本,以使其可以在一页上的多个位置使用。当您每页的倒计时次数超过时,它将无法正常工作。 提前致谢!     

解决方法

        PHP文件正在生成针对特定ID的JavaScript:
document.getElementById(\'cd\')
页面上应该只有一个ID,因此定位多个ID将会中断。您可能想切换到不同的ID和多个countdown.php文件作为一种快速解决方案,或者重写PHP文件,以使其以从客户端作为GET参数传入的ID为目标。 就是说,我强烈建议您放弃整个解决方案。 PHP脚本不会清除或验证输入。我可以通过操纵GET字符串在您的Web服务器上运行代码。这似乎也没有必要的开销-这一切都可以在客户端完成。 这是我认为您应该考虑的jQuery倒计时插件,它确实具有时区支持。