如何在PHP GIF倒数计时器中获取时区?

时间:2018-08-29 12:33:35

标签: php html gif countdowntimer

我创建了一个GIF LiveTimer,代码如下。

默认时区为“澳大利亚/墨尔本”,其硬编码在 gif.php 文件中。

运行计时器GIF示例如下。我想要动态时区的地方。 http://gifcountdowntimergenrator.atwebpages.com/module/gif.php?time=2018-09-06+17:58:28

  

gif.php

<?php

date_default_timezone_set('Australia/Melbourne');

function tz_list() {
  $zones_array = array();
  $timestamp = time();
  foreach(timezone_identifiers_list() as $key => $zone) {
    date_default_timezone_set($zone);
    $zones_array[$key]['zone'] = $zone;
    $zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date('P', $timestamp);
  }
  return $zones_array;
}

include 'GIFEncoder.class.php';
include 'php52-fix.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('../images/timer.png');
$delay = 100; // milliseconds
$font = array(
    'size' => 50, // Font size, in pts usually.
    'angle' => 0, // Angle of the text
    'x-offset' => 23, // The larger the number the further the distance from the left hand side, 0 to align to the left.
    'y-offset' => 70, // The vertical alignment, trial and error between 20 and 60.
    'file' => __DIR__ . DIRECTORY_SEPARATOR . '../fonts/arial.ttf', // Font path
    'color' => imagecolorallocate($image, 0, 36, 150), // RGB Colour of the text
);
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('../images/timer.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('../images/timer.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();

?>

我希望每次通过UI选择时区时都会动态更新该时区,屏幕截图如下

timer.php

timer.php

我在生成时区的代码下面

<?php
            /**
             * Timezones list with GMT offset
             *
             * @return array
             * @link http://stackoverflow.com/a/9328760
             */
            function tz_list() {
              $zones_array = array();
              $timestamp = time();
              foreach(timezone_identifiers_list() as $key => $zone) {
                date_default_timezone_set($zone);
                $zones_array[$key]['zone'] = $zone;
                $zones_array[$key]['diff_from_GMT'] = 'UTC/GMT ' . date('P', $timestamp);
              }
              return $zones_array;
            }
            ?>

           <select>
                <option value="0">Please, select timezone</option>
                <?php foreach(tz_list() as $t) { ?>
                  <option value="<?php print $t['zone'] ?>">
                    <?php print $t['diff_from_GMT'] . ' - ' . $t['zone'] ?>
                  </option>
                <?php } ?>
          </select>

但是问题是,如果我使用的是 POST 方法,则GIF PHP无法正常工作,并且图像也不会生成。

有没有办法动态获取时区,而不会影响结果

gif.php

gif.php

0 个答案:

没有答案