# Duration help

3 replies to this topic

### #1 DieSucker

DieSucker
• Members
• Member
• 22 posts

Posted 24 June 2006 - 03:11 PM

So i have this duration 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;
}```

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?

### #2 ale_jrb

ale_jrb
• Members
• Member
• 17 posts
• LocationLondon, UK

Posted 24 June 2006 - 04:00 PM

```
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}"; }

```

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.

### #3 DieSucker

DieSucker
• Members
• Member
• 22 posts

Posted 24 June 2006 - 04:11 PM

thanks alot, forgot the \$ in min and hours, but got it workin, thanks alot

### #4 ale_jrb

ale_jrb
• Members
• Member
• 17 posts
• LocationLondon, UK

Posted 24 June 2006 - 04:54 PM

Oops lol - glad it helped.

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users