Web Analytics Made Easy -
StatCounter How to show a message when a countdown timer reaches zero in PHP? - CodingForum

Announcement

Collapse
No announcement yet.

How to show a message when a countdown timer reaches zero in PHP?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to show a message when a countdown timer reaches zero in 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();`

  • #2
    All of the PHP gets run before anything displays in the browser so you'd need to have the animated gif display the message after performing the countdown and before it loops around to start the countdown again.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

    Comment


    • #3
      Good stuff felgall! But how would I code that into the snippet I have above? I found an example that is close to what I'm looking for but just don't understand where I would start with adding it in.

      Comment


      • #4
        Hey Felgall, would you know how to implement the feature I mentioned yesterday into the timer?

        Comment

        Working...
        X