doumanni3501 2018-08-29 12:33
浏览 71

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

I have created a GIF LiveTimer and the code is below.

Where default timezone is 'Australia/Melbourne' which is hardcoded in gif.php file.

Running Timer GIF Example is below. Where I want dynamic timezone. 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();

?>

I want this timezone to be updated dynamically whenever I select timezone through UI, screenshot is below

timer.php

timer.php

I am below code to generate Timezones

<?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>

But the problem is that if I am using POST method then GIF PHP is not working then and image is not generating.

Is there any way to get timezone in dynamically which would not effect the result

gif.php

gif.php

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 vhdl+MODELSIM
    • ¥20 simulink中怎么使用solve函数?
    • ¥30 dspbuilder中使用signalcompiler时报错Error during compilation: Fitter failed,求解决办法
    • ¥15 gwas 分析-数据质控之过滤稀有突变中出现的问题
    • ¥15 没有注册类 (异常来自 HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
    • ¥15 知识蒸馏实战博客问题
    • ¥15 用PLC设计纸袋糊底机送料系统
    • ¥15 simulink仿真中dtc控制永磁同步电机如何控制开关频率
    • ¥15 用C语言输入方程怎么
    • ¥15 网站显示不安全连接问题