#### Archived

This topic is now archived and is closed to further replies.

# Duration help

## Recommended Posts

So i have this duration code

[code]
function duration(\$timestamp) {

\$years=floor(\$timestamp / (60*60*24*365));
\$timestamp%=60*60*24*365;

\$weeks=floor(\$timestamp / (60*60*24*7));
\$timestamp%=60*60*24*7;

\$days=floor(\$timestamp / (60*60*24));
\$timestamp%=60*60*24;

\$hrs=floor(\$timestamp / (60*60));
\$timestamp%=60*60;

\$mins=floor(\$timestamp / 60);
\$secs=\$timestamp % 60;

\$str="";

if (\$years >= 1) { \$str.="{\$years} years "; }
if (\$weeks >= 1) { \$str.="{\$weeks} weeks "; }
if (\$days >= 1) { \$str.="{\$days} days "; }
if (\$hrs >= 1) { \$str.="{\$hrs} hours "; }
if (\$mins >= 1) { \$str.="{\$mins}"; }
if (\$secs >= 10) { \$str.=":{\$secs}"; }
if (\$secs < 10) { \$str.=":0{\$secs}"; }

return \$str;
}[/code]

which I am using for unixdate. Right now the out put would be like "1:45" and for just seconds it would output
":45" my problem is how do I get it to add "00" before the colon if the duration is just seconds
Any ideas?

##### Share on other sites

[code]

if (\$years >= 1) { \$str.="{\$years} years "; }
if (\$weeks >= 1) { \$str.="{\$weeks} weeks "; }
if (\$days >= 1) { \$str.="{\$days} days "; }
if (\$hrs >= 1) { \$str.="{\$hrs} hours "; }
if (\$mins >= 1) { \$str.="{\$mins}"; }

// Checks if there are only seconds.
if (\$years < 1 && \$weeks < 1 && \$days < 1 && hrs < 1 && mins < 1)
{\$str = "00";}
if (\$secs >= 10) { \$str.=":{\$secs}"; }
if (\$secs < 10) { \$str.=":0{\$secs}"; }

[/code]

This will first work as you wrote it. When it reaches the comment, it checks to see if there are no years, weeks, days, hours or minutes. If there are not, it will set \$str to "00" before finishing the script (then the seconds and colon will be added after the 00, which is what you wanted). If there are any of the above, it will do nothing and proceed as it normally would.

Hope this helps.

##### Share on other sites
thanks alot, forgot the \$ in min and hours, but got it workin, thanks alot :D

##### Share on other sites
Oops lol - glad it helped.