View Full Version : Another date question (countdown)
Dennis_Gull
04-21-2007, 05:13 PM
Hello once again, sorry for posting so many questions but I both love php and this place :D
I store dates in my db for example: 2007-04-23 19:30:01
And I want it to show the time left until it hit zero (a countdown) but I cant figure out how to do this.
The date is separated so its.
Year = 2007
Month = 04
Day = 23
and so on..
I know I can get the current GMT with gmdate but i need some way to get the time between the time from the db and the current time.
Im not looking for an actual script that countdown automatic I just want it to countdown once when I reload the page.
Something like, 3 days and 10 hours left.
mwinter
04-21-2007, 09:50 PM
I store dates in my db for example: 2007-04-23 19:30:01
And I want it to show the time left until it hit zero (a countdown) but I cant figure out how to do this.
I know I can get the current GMT with gmdate but i need some way to get the time between the time from the db and the current time.
Using the mktime (http://uk.php.net/manual/en/function.mktime.php) (and gmmktime (http://uk.php.net/manual/en/function.gmmktime.php)) function, one can convert a specific date and time into a timestamp. The time (http://uk.php.net/manual/en/function.time.php) function returns a timestamp for the current date and time. Subtracting one from the other will yield an integer that indicates the interval (in seconds) between those two moments.
If you haven't been directed there yet, have a look at the Date and Time reference (http://uk.php.net/manual/en/ref.datetime.php) which lists the time-related functions available in PHP.
Mike
Dennis_Gull
04-23-2007, 10:31 AM
Thanks for the reply, my site was down for about 2 days so couldnt work with it.. anyways I have a function that use mktime and found out how to get it to work.. although I can only display "days left" yet but will probably find some good code if try to search some more, this is what I use now:
$target = sprintf(tep_date_left($product_info['products_date_available'])) ;
$today = time () ;
$difference =($target-$today) ;
$days =(int) ($difference/86400) ;
echo ' <font color="#605f5f">(' . $days . ' days left)</font>';
PS:
What does (int) mean? :)
Edit:
Made my own code that will get the the gmt time left in days and hours and minutes it looks like this atm: (hope I didnt do something unnecessary :))
$target = sprintf(tep_date_left($product_info['products_date_available'])) ;
$hour=gmdate('H');
$min=gmdate('i');
$sec=gmdate('s');
$month=gmdate('n');
$day=gmdate('d');
$year=gmdate('Y');
$stamp=mktime ($hour,$min,$sec,$month,$day,$year);
$left=$target-$stamp;
$days_left=floor($left/(24 * 60 * 60));
$hours_left=floor($left/(60 * 60 ));
$fix_hour=$days_left*86400;
$hour=$left-$fix_hour;
$hours_left=floor($hour/(60 * 60 ));
$fix_minute=$fix_hour+$hours_left*3600;
$minute=$left-$fix_minute;
$minute_left=floor($minute/60);
if ($days_left == 1) {
$real_d = $days_left . ' day ';
} else {
$real_d = $days_left . ' days ';
}
if ($hours_left == 1) {
$real_h = $hours_left . ' hour ';
} else {
$real_h = $hours_left . ' hours ';
}
if ($minute_left == 1) {
$real_m = $minute_left . ' minute';
} else {
$real_m = $minute_left . ' minutes';
}
$time_left = $real_d . $real_h . $real_m;
echo ' <font color="#605f5f">(' . $time_left . ')</font>';
mwinter
04-28-2007, 01:27 PM
$target = sprintf(tep_date_left($product_info['products_date_available'])) ;
I'm curious why you're using the sprintf (http://uk.php.net/manual/en/function.sprintf.php) function. Its purpose is to format a string with the addition of arguments that are inserted into the string. Calling the function with only one string argument would seem to be quite redundant. What exactly does tep_date_left return, and what's the value obtained from the $product_info associative array?
$today = time () ;
$difference =($target-$today) ;
It's also somewhat odd to use sprintf if your intention is to perform arithmetic as the function returns a string, not a number.
echo ' <font color="#605f5f">(' . $days . ' days left)</font>';[/code]
Don't use the font element. Ever. If you must use an in-line element - if you can't style the containing element - use a span element:
.countdown {
background: /* an appropriate background colour */;
color: #605f5f;
}
<span class="countdown">(<?php echo $days; ?> days left)</span>
What does (int) mean?
It is a cast. It forces a variable to be treated as a particular type; an integer, in this case. The PHP manual briefly describes casting (http://uk.php.net/manual/en/language.types.type-juggling.php#language.types.typecasting) in the type juggling (http://uk.php.net/manual/en/language.types.type-juggling.php) section. The effects of casting varies based on the cast, and the type being cast. For example, when casting to an integer (http://uk.php.net/manual/en/language.types.integer.php#language.types.integer.casting), booleans will become one (1) or zero (0), and real (floating-point) numbers will be truncated (rounded down). The treatment of strings and the other remaining types are more complicated - see the manual for more information.
$hour=gmdate('H');
$min=gmdate('i');
$sec=gmdate('s');
$month=gmdate('n');
$day=gmdate('d');
$year=gmdate('Y');
$stamp=mktime ($hour,$min,$sec,$month,$day,$year);
This is unnecessary. Just obtain the timestamp from the time function. The timestamp is in local time, just as the one returned by mktime (http://uk.php.net/manual/en/function.mktime.php) will be.
If there ever is a time when you need to make several calls to obtain information about a particular moment in time, be sure to record that time first and act on that stored timestamp; though it's unlikely, it's possible for the system time to change between calls resulting in inaccurate data. In the example above, the gmdate (http://uk.php.net/manual/en/function.gmdate.php) function takes a timestamp as a second argument. However, even that is excessive: the getdate (http://uk.php.net/manual/en/function.getdate.php) function will return an associative array containing information about the current local time, or some other specific moment.
Here's an alternative version of your code. The argument, $target, is assumed to be an integer timestamp. The second, optional argument will be returned if the target time has been reached, or has already passed; this defaults to null. If less than sixty seconds remain, the return value is the string "less than a minute".
define('SECONDS_PER_DAY', 86400);
define('SECONDS_PER_HOUR', 3600);
define('SECONDS_PER_MINUTE', 60);
function constructCountdown($target, $reached = null) {
$remainingTime = $target - time();
if ($remainingTime <= 0) return $reached;
else if ($remainingTime < SECONDS_PER_MINUTE) return 'less than a minute';
$remainingDays = (int) ($remainingTime / SECONDS_PER_DAY);
$temp = $remainingTime - ($remainingDays * SECONDS_PER_DAY);
$remainingHours = (int) ($temp / SECONDS_PER_HOUR);
$temp -= $remainingHours * SECONDS_PER_HOUR;
$remainingMinutes = (int) ($temp / SECONDS_PER_MINUTE);
if ($remainingDays > 0) {
$result = "$remainingDays day";
if ($remainingDays != 1) $result .= 's';
} else {
$result = '';
}
if (($remainingHours > 0) || (strlen($result) > 0)) {
if (strlen($result) > 0) $result .= ', ';
$result .= "$remainingHours hour";
if ($remainingHours != 1) $result .= 's';
}
if (($remainingMinutes > 0) || (strlen($result) > 0)) {
if (strlen($result) > 0) $result .= ', and ';
$result .= "$remainingMinutes minute";
if ($remainingMinutes != 1) $result .= 's';
}
return $result;
}
Mike
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.