当倒数计时器在PHP中达到零时如何显示消息?

时间:2016-09-01 22:30:40

标签: php timer countdown

我正在研究一个用PHP构建的倒数计时器。我想弄清楚如何在倒计时器达到零之后添加一条说“EXPIRED”的消息。任何人都可以帮助我将此功能添加到倒数计时器中吗?我已经列出了下面的代码。

<?php

date_default_timezone_set('Australia/ACT');
include 'GIFEncoder.class.php';

$time = $_GET['time'];
$future_date = new DateTime(date('r',strtotime($time)));
$time_now = time();
$now = new DateTime(date('r', $time_now));

$frames = array();
$delays = array();

$image = imagecreatefrompng('countdown.png');
$delay = 100; // milliseconds
$font = array(
    'size'=>73,
    'angle'=>0,
    'x-offset'=>10,
    'y-offset'=>70,
    'file'=>'Transist.ttf',
    'color'=>imagecolorallocate($image, 243, 139, 0),
    'color2'=>imagecolorallocate($image, 217, 0, 0),
);
for($i = 0; $i <= 60; $i++){
    $interval = date_diff($future_date, $now);
    if($future_date < $now){
        // Open the first source image and add the text.
        $image = imagecreatefrompng('countdown.png');;
        $text = $interval->format('00:00:00:00');
        imagettftext ($image , $font['size'] , $font['angle'] , $font['x-offset'] , $font['y-offset'] , $font['color'] , $font['file'], $text );
       ob_start();
       imagegif($image);
       $frames[]=ob_get_contents();
       $delays[]=$delay;
           $loops = 1;
       ob_end_clean();
       break;
    } else {
      // Open the first source image and add the text.
      $image = imagecreatefrompng('countdown.png');;
      $text = $interval->format('%a:%H:%I:%S');
      // %a is weird in that it doesn’t give you a two digit number
      // check if it starts with a single digit 0-9
      // and prepend a 0 if it does
      if(preg_match('/^[0-9]\:/', $text)){
         $text = '0'.$text;
      }
      imagettftext ($image , $font['size'] , $font['angle'] , $font['x-offset'] , $font['y-offset'] , $font['color'] , $font['file'], $text );
      ob_start();
      imagegif($image);
      $frames[]=ob_get_contents();
      $delays[]=$delay;
          $loops = 0;
      ob_end_clean();
      } 
    $now->modify('+1 second');
}

//expire this image instantly
header( 'Expires: Sat, 26 Jul 1997 05:00:00 GMT' );
header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s' ) . ' GMT' );
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Cache-Control: post-check=0, pre-check=0', false );
header( 'Pragma: no-cache' );
$gif = new AnimatedGif($frames,$delays,$loops);
$gif->display();`

1 个答案:

答案 0 :(得分:0)

javascript中的setInterval函数:

<script>
    setInterval(function () {
       .... do something every 1000 milliseconds ....
    }, 1000); 
</script>

你可以让它来更新你的计时器...如果这是你想要的。根据需要将1000改为更慢/更快的东西。