
Originally Posted by
Dennis_Gull
$target = sprintf(tep_date_left($product_info['products_date_available'])) ;
I'm curious why you're using the sprintf 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:
Code:
.countdown {
background: /* an appropriate background colour */;
color: #605f5f;
}
HTML Code:
<span class="countdown">(<?php echo $days; ?> days left)</span>
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 in the type juggling section. The effects of casting varies based on the cast, and the type being cast. For example, when casting to an integer, 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 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 function takes a timestamp as a second argument. However, even that is excessive: the getdate 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".
PHP Code:
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
Bookmarks