php语法如何做一个动态倒计时

  

如何利用php语法做一个动态倒计时的效果,下面编程教程网小编给大家简单介绍一下具体实现代码!

具体代码如下:

function getSecondsLeft($targetTime)
{
  $currentTime = time();
  return strtotime($targetTime) - $currentTime;
}

//转换时间
function formatSeconds($num)
{
  $day = floor($num / (3600 * 24));
  $hours = floor(($num % (3600 * 24)) / 3600);
  $minutes = floor(($num % 3600) / 60);
  $seconds = $num % 60;
 
  return array($day, $hours, $minutes, $seconds);
}

//输出倒计时
function displayCountdown($targetTime)
{
  $seconds = getSecondsLeft($targetTime);
  $timeArray = formatSeconds($seconds);
 
  $countDown = "";
 
  if ($timeArray[0] > 0) {
    $countDown .= $timeArray[0] . "天";
  }
 
  $countDown .= str_pad($timeArray[1], 2, "0", STR_PAD_LEFT) . ":";
  $countDown .= str_pad($timeArray[2], 2, "0", STR_PAD_LEFT) . ":";
  $countDown .= str_pad($timeArray[3], 2, "0", STR_PAD_LEFT);
 
  echo $countDown;
}
以上是编程学习网小编为您介绍的“php语法如何做一个动态倒计时”的全面内容,想了解更多关于 php入门 内容,请继续关注编程基础学习网。

相关文章