Jump to content

Countdown seqence from value entered by user


thebl

Recommended Posts

For an assignment I'm trying to get the following to work. For example if a user enter a specific number, such as 5, this will equal to 5 hours. And what I want it to is to begin a countdown from the value entered by the user.So far I've been only able to get a countdown to work with the values already being specified. What I want is to begin a countdown from the value entered by the user.

<?php$timeleft = $_GET["timeleft"]; //Value entered by user$target = mktime(0, 0, 0, 9, 23, 2007);$today = time ();$difference =($target-$today);$days =(int) ($difference/86400);$hours =(int) (($difference - $days * 86400) / 3600);$minutes =(int) (($difference - $days * 86400 - $hours * 3600) / 60);$seconds =(int) ($difference - $days * 86400 - $hours * 3600 - $minutes * 60);print "Time left $days day(s) and $hours hours : $minutes minutes  : $seconds seconds";?>

Link to comment
Share on other sites

I've been playing with the following code with little success. What I want it to do begin a count down from the duration of 5 hours So for a duration 5 hours, I want to get a countdoen of the time, each time the page is refreshed, such as 4:59:11 and so forthI know I missing something major in the code, thats why I'm asking for help!!!

<?php $duration = 5; echo "<p>";$target = mktime(0+duration, 0, 0, date("m"), date("d"), date("y"));$today = time ();$difference =($target-$today);$days =(int) ($difference/86400);$hours =(int) (($difference - $days * 86400) / 3600);$minutes =(int) (($difference - $days * 86400 - $hours * 3600) / 60);$seconds =(int) ($difference - $days * 86400 - $hours * 3600 - $minutes * 60);echo "<p>Time left is $hours:$minutes:$seconds"; ?>

Link to comment
Share on other sites

First off, you never need to add 0 to something. It doesn't do anything. Also, you aren't using the variable in mktime, you left off the $. But look at what you're telling mktime to do, you are telling it to generate a timestamp for 5am of the current day. Then you get the current timestamp in $today, and take the difference. The difference will most often be negative unless you run the code before 5am. So really all that code does is tell you how long away it is from 5am on the current day.If you want to count down from a certain point, get the timestamp to countdown from and save it in a cookie. Each time the page loads check if the cookie exists, and if not create a new timestamp and save it in the cookie. If it does exist, get the timestamp from the cookie and then compute the difference based on that.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...