Countdown timer with php
Working with time in php is kicking my butt.
I'm trying to make a timer that starts when I click a button. I'm hoping to make something that, when I click a button, enters a current time and a time + 5 minutes into a database, and then outputs the difference in min:sec. I need to store the times so they include date so that when the site is not in use, it doesn't show up some weird value.
The first page enters the times
$target = date('Y-m-d H:i:s') + date('i', 5);
$starttime = date('Y-m-d H:i:s');
INSERT INTO countdowntimer (target, starttime) VALUES ('$target', '$starttime');
And the second page displays it:
$starttime = mysql_query("SELECT starttime FROM countdowntimer");
$target = mysql_query("SELECT target FROM countdowntimer");
$difference = $target-$starttime
$min = floor($difference/60)
$sec = floor($difference-($min*60))
if ($difference < 0) {
echo '0:00'; }
开发者_如何学Goelse {
echo $min . ':' . $sec; }
Well, my $target
is getting entered as 0000-00-00 00:00:00 and my display page shows "0.1". The variables are $startime=Resource id #3, $target=Resource id#4, $difference=1, $min=0, $sec=1.
First question: how do I enter the current time + 5 minutes Second question: Do you have any tips on working with the values to be displayed as min:sec. I'd like the min to show up as 1 digit and then 0, and the seconds, when less than 10 displayed as :09, :08, etc..
Thanks.
mysql_query()
returns a mysql result object, not the value you're selecting from the database. The proper code sequence would be
$result = mysql_query("... your query here ...") or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
$value = $row[0];
} else {
... query didn't return anything
}
The date math could be as follows:
$starttime = time(); // PHP's date values are simply "seconds since jan 1/19170"
$target = $now + (5 * 60);
$query = "INSERT INTO countdowntimer (target, starttime) VALUES (from_unixtimestamp($target), from_unixtimestamp($starttime));"
... query stuff here ...
or if you want to do it purely within mysql:
INSERT INTO countdowntimer (target, starttime) VALUES (DATE_ADD(now(), INTERVAL 5 MINUTE), now())
Then later on:
$query = "SELECT target, starttime FROM countdowntimer";
$result = mysql_query($query) or die(mysql_error());
$row = mysql_fetch_assoc($result);
$target = $result['target'];
$starttime = $result['target'];
and so on.
$min = (int)($difference / 60); // get minutes
$sec = $difference % 60; // get the remainder
$fancy_string = sprintf('%d:%02d', $min, $sec);
A couple of ways might work.
One way is strtotime: http://php.net/manual/en/function.strtotime.php
You can express it like:
$timestamp = strtotime('+ 5 minutes', $target);
Or you can use mktime to add time:
$date = date_parse($target);
//the above puts your date into an array
$timestamp = mktime($date['hour'], ($date['minutes'] + 5), $date['seconds'], $date['month'], $date['day'], $date['year']);
//now you can format your date with the date function
date('F d, Y h:i:s', $timestamp);
精彩评论