It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum

Count down timer from now to a specific time

Moderator: Malcolm

Count down timer from now to a specific time

Postby john_zakaria on Wed Dec 02, 2009 9:57 am

i enters date and time into my database table.. after that i select this time and date in a query.
all that i need is to countdown the timer from time now to the date that i select from databse

example:
i got from database the time in which the data is inserted:

$result = 2009-12-02 05:34:58;

now i want to count down the time from now

2009-12-02 06:34:58;

to $result


so it will count 01:00:00 too 00:00:00


Note : i wnt the countdown timer to be shown to the user i dont want to tell him that there is one hour left, i want to show the time that counts from now to result.

can anyone help me in doing this module?
john_zakaria
50+ Club
 
Posts: 59
Joined: Sun Feb 08, 2009 7:29 am

Re: Count down timer from now to a specific time

Postby dflynn on Tue Dec 29, 2009 4:37 am

If you want this to run live on the website, instead of just when the page is refreshed, you'd be best off using AJAX.

When the script is called by the browser, on page load, you need to run a query to get the time from the database

What you might want to use is date('U'); to get the number of seconds since Jan 1, 1970, 00:00:00 and add the number of seconds needed:

Code: Select all
<?php
//Get the number from the DB
$result = 1262059807;
//1 hour  3600 seconds
$limit  = $result+3600; //1262063407
//Get the time now to compare against
$time_now = date('U'); //1262059897

//subtract the $limit from the $time_now and multiply by -1 for a positive integer
$time_left= ($time_now - $limit)*(-1);
//$time_left = 1262059897-1262063407= 3510

//divide the $time_left by 60 seconds to get the number of minutes remaining
//use floor(); to round down, else you might display an extra minute by accident.
$minutes = floor($time_left/60); //floor(3510/60) = 58
//multiply the total $minutes by 60 and subtract it from the time left to get seconds.
$seconds = ($time_left)-($minutes*60); //58*60 = 3480 :: 3510-3480 = 30 seconds.

echo $minutes.":".$seconds;
?>


This should do it. It's not tested. If you have any troubles give me a shout.
User avatar
dflynn
500+ Club
 
Posts: 860
Joined: Wed Oct 03, 2007 9:06 pm
Location: Guelph, Canada


Who is online

Users browsing this forum: No registered users and 0 guests