PHP countdown with hours, minutes and seconds

Have a question or a suggestion about a 3rd party addon module or plugin?
Let us know here.
Post Reply
daxing
New Member
New Member
Posts: 1
Joined: Thu Sep 03, 2015 4:45 am

PHP countdown with hours, minutes and seconds

Post 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;
thefrozen
Forum Members
Forum Members
Posts: 10
Joined: Sat Dec 07, 2013 5:04 pm

Re: PHP countdown with hours, minutes and seconds

Post 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;
Post Reply

Return to “Modules/Add-Ons”