# Thread: simple timestamp problem [HELP!]

Join Date
Feb 2005
Posts
114

## simple timestamp problem [HELP!]

I have this function I wrote, but it seems to always get thrown into an infinite loop. the problem is I'm trying to convert a integer (i.e. 135533234) into a human readable time stamp. (ie. 4:23:31:10). The integer is in seconds. This is what I have so far, can someone help me with possibly an easier way to do this, or help me fix this one?

PHP Code:
``` function htime(\$totaltime) { \$seconds = 0; \$minutes = 0; \$hours = 0; \$days = 0; while (\$totaltime >= 0) { if (\$totaltime >= 86400) {     \$days++;     \$totaltime - 86400;     } elseif (\$totaltime  >= 3600) {         \$hours++;         \$totaltime - 3600;         } elseif (\$totaltime >= 60) {             \$minutes++;             \$totaltime - 60;             } elseif (\$totaltime >= 1) {                 \$seconds++;                 \$totaltime - 1;                 }             } \$final = \$days.":".\$hours.":".\$minutes.":".\$seconds; return \$final; }  ```

2. Newbie
Join Date
Jun 2005
Posts
7
What is the integer? If it's a Unix timestamp you can just use the time() function...

3. Junior Guru Wannabe
Join Date
Apr 2003
Location
Sunny UK
Posts
33
There are probably easier ways to do this but I'll stick with your code.

First thing is that your negation syntax isn't right - you need to have:

\$totaltime -= 86400;

and so on for all your negations (that's the same as putting \$totaltime = \$totaltime - 86400; )

But yeah, you do get stuck in an infinite loop because once \$totaltime gets to 0, none of the if conditions apply but the while loop condition still does. Just change the while condition to:

while (\$totaltime > 0)

and you should be sorted.

4. Web Hosting Evangelist
Join Date
Apr 2005
Posts
521
David Clain, by looking at the problem explanation and return value it's concludable that Atomic Haven wants to display number of days, hours etc. for a given timestamp so issuing time() function is pointless.

Atomic Haven, you didn't clearly specify what exactly do you want to do and where do you get that integer from. Is it a Unix timestamp or just plain represnentation of an amount of time within one month (or whatever)?

Anway, if I had to count number of days from an integer, i'd probably divide numbers ( round(\$totaltime / (3600*24)) ) and so on for every possible time representation (hour, minute, second).

Join Date
Feb 2005
Posts
114
basically I have two time stamps. One is larger than the other, I get a Number when I subtract the smaller one from the larger one. I then want to take that number (which is in seconds) and see how many days, hours, minutes and seconds there are.

6. Web Hosting Master
Join Date
Dec 2004
Location
Posts
1,082
This is a snippet I end up using quite often.

PHP Code:
``` function prettyInterval(\$seconds = 0) {    \$f_seconds = \$seconds - intval(\$seconds);    \$days = intval(\$seconds / 86400);    \$seconds %= 86400;    \$hours = intval(\$seconds / 3600);    \$seconds %= 3600;    \$minutes = intval(\$seconds / 60);    \$seconds %= 60;    \$seconds = intval(\$seconds);    return (\$days > 0 ? "\$days days, " : "") . \$hours . ":" . sprintf("%02d", \$minutes) . ":" . sprintf("%02d", \$seconds) . "." . sprintf("%d", \$f_seconds * 1000); }  ```

Join Date
Feb 2005
Posts
114
That worked like a charm, thank you!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•