dpjhq00684 2016-09-01 22:30
浏览 131

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

I'm working on a countdown timer that's built with PHP. I'm trying to figure out how to add a message that says "EXPIRED" after the countdown timer reaches zero. Can anyone help me with adding this feature into the countdown timer? I've listed the code below.`

<?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条回答 默认 最新

  • ds3422222222 2016-09-01 22:44
    关注

    The setInterval function in javascript:

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

    You can get it to update your timer ... if that is what you are wanting. Change the 1000 to something slower/faster as you wish.

    评论

报告相同问题?

悬赏问题

  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 关于无人驾驶的航向角
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了