Page 1 of 1

PHP countdown with hours, minutes and seconds

Posted: Thu Sep 03, 2015 4:59 am
by daxing
Trying to figure out how to create a UDT that displays hours, minutes and seconds (ex. 00:05:20).
I'm going after the script posted on here a few years ago. Been trying to modify it by adding seconds, but I'm not good enough to figure out what I'm doing wrong. Thankful for any help :)

Code: Select all

$year = $params['year'];
$month = $params['month'];
$day = $params['day'];
$hour = $params['hour'];
$minute = $params['minute'];
$countdown = "There are ";

$the_countdown_date = mktime($hour, $minute, 0, $month, $day, $year);

$today = time();

$difference = $the_countdown_date - $today;
if ($difference < 0) $difference = 0;

$days_left = floor($difference/60/60/24);
$hours_left = floor(($difference - $days_left*60*60*24)/60/60);
$minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);

$countdown .= $days_left." days ";
$countdown .= $hours_left." hours and ";
$countdown .= $minutes_left." minutes left.";

echo $countdown;

Re: PHP countdown with hours, minutes and seconds

Posted: Tue Sep 08, 2015 2:53 pm
by thefrozen
Adding seconds is not the problem.

I guess you know, that the seconds are never updated until the page reloads... ;)

Code: Select all

$year = $params['year'];
$month = $params['month'];
$day = $params['day'];
$hour = $params['hour'];
$minute = $params['minute'];
$second = $params['second'];
$countdown = "There are ";

$the_countdown_date = mktime($hour, $minute, $second, $month, $day, $year);

$today = time();

$difference = $the_countdown_date - $today;
if ($difference < 0) $difference = 0;

$days_left = floor($difference/60/60/24);
$hours_left = floor(($difference - $days_left*60*60*24)/60/60);
$minutes_left = floor(($difference - $days_left*60*60*24 - $hours_left*60*60)/60);
$seconds_left = floor($difference%60);

$countdown .= $days_left." days ";
$countdown .= $hours_left." hours ";
$countdown .= $minutes_left." minutes and ";
$countdown .= $seconds_left." seconds left.";

echo $countdown;