Jump to content

Count down?


Freid001

Recommended Posts

This script uses php and javascript, and I set it to 30 seconds for you.  It uses sessions so that if you refresh the page, it will keep the countdown.  After reaching 0 it will remain, until a page refresh, then it returns to the 30 second countdown.

 

<?php
session_start();
$timestamp = time();
$diff = 30;
if(isset($_SESSION['ts'])) {
$slice = ($timestamp - $_SESSION['ts']);	
$diff = $diff - $slice;
}

if(!isset($_SESSION['ts']) || $diff > 30 || $diff < 0) {
$diff = 30;
$_SESSION['ts'] = $timestamp;
}

//Below is demonstration of output.  Seconds could be passed to Javascript.
$diff; //$diff holds seconds less than 3600 (1 hour);

$hours = floor($diff / 3600) . ' : ';
$diff = $diff % 3600;
$minutes = floor($diff / 60) . ' : ';
$diff = $diff % 60;
$seconds = $diff;


?>
<div id="strclock">Clock Here!</div>
<script type="text/javascript">
var hour = <?php echo floor($hours); ?>;
var min = <?php echo floor($minutes); ?>;
var sec = <?php echo floor($seconds); ?>

function countdown() {
if(sec <= 0 && min > 0) {
  sec = 59;
  min -= 1;
}
else if(min <= 0 && sec <= 0) {
  min = 0;
  sec = 0;
}
else {
  sec -= 1;
}

if(min <= 0 && hour > 0) {
  min = 59;
  hour -= 1;
}

var pat = /^[0-9]{1}$/;
sec = (pat.test(sec) == true) ? '0'+sec : sec;
min = (pat.test(min) == true) ? '0'+min : min;
hour = (pat.test(hour) == true) ? '0'+hour : hour;

document.getElementById('strclock').innerHTML = hour+":"+min+":"+sec;
setTimeout("countdown()",1000);
}
countdown();
</script>

Link to comment
Share on other sites

well as mentioned by the poster of the script it requires both PHP and Javascript to run. One thing the person failed to mention is with the script posted you need a display element on your page hidden or otherwise with the id="" part of the element being id="strclock"

 

what the php part of the script does is break the time down and find the difference between the given time and the count down time then echoed into the value area of the given element with id="strclock" as its ID. The JavaScript will then find the information within that element the way the JavaScript is devices I would suggest using a div as your display element. And will simulate a countdown visually of the desired countdown you want.

 

In a matter of opinion I think this is a bit over kill.

 

I would say depending on what your trying to do with a countdown your best bet would be just to use javascript completely to handle what you want to do.

 

PHP only runs once and thats while the page is loading. Its constructing the given page to be displayed server side. Where as Javascript can manipulate a page as much as you want after the page has been loaded client side.

Link to comment
Share on other sites

Im almost positive they have something here..

http://dynamicdrive.com/

 

This sites been around for years has a bunch of canned code layin about ripe for the picking. Most of the code there is dated but still working, others leave room for improvement. But you can deffinately find what your looking for there.. worse comes to worse, google is very handy just key in "Javascript Countdown"

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.