我试图设置一个同步的促销倒数计时器,它会继续使用JavaScript倒计时。
但每次刷新页面时都应始终使用PHP检索开始时间。
每次刷新页面等待时间总是相同的,即使已经过了30秒。
$left
几乎没有正常工作
<?php
$promotionEnds = 'May 31, 2014, 11:59 pm'; //Enter date for promotion
$left = date("Y-m-d h:i:s", strtotime($promotionEnds, strtotime("now")));
echo "Time = ". strtotime($promotionEnds)."\r\n";
echo "Now Time = ". strtotime("now")."\r\n";
echo "Countdown = ".$left."\r\n";
?>
我也可以进入Y = 0 m = 0 d = 0 h = 0 i = 0 s = 0
吗?
或者我必须使用unix时间戳秒
这是一些输出
Time = 1401566340
Now Time = 1401234087
Countdown = 2014-05-31 11:59:00
Time = 1401566340
Now Time = 1401234098
Countdown = 2014-05-31 11:59:00
Time = 1401566340
Now Time = 1401234111
Countdown = 2014-05-31 11:59:00
这是我尝试过的新内容。
<?php
$endPromotionSeconds = strtotime($promotionEnds);
$days = floor((time() - $endPromotionSeconds) / 86400);
$hours = floor((time() - $endPromotionSeconds) / 3600);
$minutes = ((time() - $endPromotionSeconds) / 60) % 60;
$seconds = (time() - $endPromotionSeconds) % 60;
echo "Countdown = d " . $days . " h " . $hours . " m " . $minutes . " s " . $seconds . "\r\n";
?>
Countdown = d -3.835625 h -3.835625 m -3.835625 s -3.835625
Countdown = d -3.8354166666667 h -3.8354166666667 m -3.8354166666667 s -3.8354166666667
最后HTML应生成
<dd>
<div class="timer_wrp">Remaining
<ul class="timer countdown">
<li><i><?php echo $daysLeft; ?></i>Day</li>
<li><i><?php echo $hoursLeft; ?></i>Hour</li>
<li><i><?php echo $minutesLeft; ?></i>Min</li>
<li><i><?php echo $secondsLeft; ?></i>Sec</li>
</ul>
</div>
</dd>
答案 0 :(得分:2)
看起来您实际上找不到现在和促销结束之间的差异以获得剩余时间。
$now = new DateTime();
$ends = new DateTime('May 31, 2014, 11:59 pm');
$left = $now->diff($ends);
<dd>
<div class="timer_wrp">Remaining
<ul class="timer countdown">
<li><i><?php echo $left->format('%a'); ?></i>Day</li>
<li><i><?php echo $left->format('%h'); ?></i>Hour</li>
<li><i><?php echo $left->format('%i'); ?></i>Min</li>
<li><i><?php echo $left->format('%s'); ?></i>Sec</li>
</ul>
</div>
</dd>