Results 1 to 7 of 7
  1. #1

    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) {
    $totaltime 86400;
        } elseif (
    $totaltime  >= 3600) {
    $totaltime 3600;
            } elseif (
    $totaltime >= 60) {
    $totaltime 60;
                } elseif (
    $totaltime >= 1) {
    $totaltime 1;
    $final $days.":".$hours.":".$minutes.":".$seconds;
    $final; - for sale!

  2. #2
    What is the integer? If it's a Unix timestamp you can just use the time() function...

  3. #3
    Join Date
    Apr 2003
    Sunny UK
    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. #4
    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).

  5. #5
    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. - for sale!

  6. #6
    Join Date
    Dec 2004
    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 "$days days, " "") . $hours ":" sprintf("%02d"$minutes) . ":" sprintf("%02d"$seconds) . "." sprintf("%d"$f_seconds 1000);

  7. #7
    That worked like a charm, thank you! - for sale!

Posting Permissions

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