0

I have a PHP countdown clock that I is going to be put on my website but am struggling to debug some errors. When the clock shows 1 minute it still says "1 Minutes" (notice the S in minutes) and it also displays "0 minutes". I have posted a copy of my code below.

<?php

countdown(2011,6,7,7,31,0);

function countdown($year, $month, $day, $hour, $minute){
$the_countdown_date=mktime($hour, $minute, 0, $month, $day, $year, -1);
$today=time();
$difference=$the_countdown_date - $today;
$days=floor($difference/60/60/24);
$hours=floor(($difference - $days*60*60*24)/60/60);
$minutes=floor(($difference - $days*60*60*24 - $hours*60*60)/60);
echo "$days days $hours hours $minutes minutes";
}

?>

Is there any way I can get it to say "1 minute" and not display the minutes section for "0 minutes"?

I hope people can understand this. Thanks in advance

Callum

3
  • The correct usage in english IS 0 minutes, not 0 minute, just FYI Commented Mar 31, 2011 at 22:13
  • rayman i think you misunderstood something or otherwise had nothing more interesting to say ^^ Commented Mar 31, 2011 at 22:19
  • No the comment was originally intended for a post that was deleted, so I put it up here for that person to see. Commented Apr 1, 2011 at 2:18

3 Answers 3

1

Create a variable that is either "s" or "" depending on the value of the corresponding days/hours/mins:

$days_s = $days == 1 ? "" : "s";
$hours_s = $hours == 1 ? "" : "s";
$minutes_s = $minutes == 1 ? "" : "s";
return "$days day$days_s $hours hour$hours_s $minutes minute$minutes_s";
Sign up to request clarification or add additional context in comments.

Comments

0

add (at top)

function nicefmt($n,$s,$trail=" "){
if($n==0) return "";
if($n==1) return "$n $s".$trail;
return "$n ".$s."s".$trail;
}

and the replace

echo "$days days $hours hours $minutes minutes";

with

echo nicefmt($days,"day").nicefmt($hours,"hour").nicefmt($minutes,"minute","");

Comments

0

Just put an if statement in that is something like:

if ($minutes == 1) {
    echo "$days days $hours hours $minutes minute";
} else if ($minutes == 0) {
    echo "$days days $hours hours";
} else {
  echo "$days days $hours hours $minutes minutes";
}

I know this is very verbose... to reduce it, I would the final string into sections and only print the sections needed.

3 Comments

$days == 0 && $hours == 0 why?
I think you meant != not ==, since the topmost condition (for example) gets executed only when there are 0 days, 0 hours and a minute.
..or use the frikkin ternary operator while stealing others' answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.