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

2025年01月16日 建站教程

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

具体代码如下:

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

本文链接:http://so.lmcjl.com/news/21455/

展开阅读全文
相关内容