Help me fix my "x minutes ago" time script please :)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JamesG

    #1

    Help me fix my "x minutes ago" time script please :)

    I'm trying to convert a unix timestamp to a readable time, such as 4
    hours ago, or 25 minutes ago.
    My code is below:

    function ago($ts) {
    /* time difference */
    $ts = (time() - $ts);

    if($ts < 60) {
    /* <1 minute */
    return $ts . " seconds ago";
    } elseif($ts < 60 * 60) {
    /* <1 hour */
    return floor($ts / 60) . " minutes ago";
    } elseif ($ts < 60 * 60 * 2) {
    /* <2 hour */
    return "1 hour ago";
    } elseif ($ts < 60 * 60 * 24) {
    /* <24 hours = 1 day */
    return floor($ts / 60 * 60) . " hours ago";
    } elseif ($ts < 60 * 60 * 24 * 2) {
    /* <2 days */
    return "1 day ago";
    } elseif ($ts < 60 * 60 * 24 * 7) {
    /* <7 days = 1 week */
    return floor($ts / 60 * 60 * 24) . " days ago";
    } elseif ($ts < 60 * 60 * 24 * 30.5) {
    /* <30.5 days ~ 1 month */
    return floor($ts / 60 * 60 * 24 * 7) . " weeks ago";
    } elseif ($ts < 60 * 60 * 24 * 365) {
    /* <365 days = 1 year */
    return floor($ts / 60 * 60 * 24 * 30.5) . " months ago";
    } else {
    /* more than 1 year */
    return floor($ts / 60 * 60 * 24 * 365) . " years ago";
    }
    }

    Everything seems to work okay, except the "hours ago" case. A time 2
    hours ago will output "9692 hours ago", or a time 18 hours ago will
    read "9692 hours ago"!

    Anyone help here? Thanks in advance

  • ZeldorBlat

    #2
    Re: Help me fix my &quot;x minutes ago&quot; time script please :)

    On Apr 17, 1:39 pm, JamesG <mailmeh...@gma il.comwrote:
    I'm trying to convert a unix timestamp to a readable time, such as 4
    hours ago, or 25 minutes ago.
    My code is below:
    >
    function ago($ts) {
    /* time difference */
    $ts = (time() - $ts);
    >
    if($ts < 60) {
    /* <1 minute */
    return $ts . " seconds ago";
    } elseif($ts < 60 * 60) {
    /* <1 hour */
    return floor($ts / 60) . " minutes ago";
    } elseif ($ts < 60 * 60 * 2) {
    /* <2 hour */
    return "1 hour ago";
    } elseif ($ts < 60 * 60 * 24) {
    /* <24 hours = 1 day */
    return floor($ts / 60 * 60) . " hours ago";
    } elseif ($ts < 60 * 60 * 24 * 2) {
    /* <2 days */
    return "1 day ago";
    } elseif ($ts < 60 * 60 * 24 * 7) {
    /* <7 days = 1 week */
    return floor($ts / 60 * 60 * 24) . " days ago";
    } elseif ($ts < 60 * 60 * 24 * 30.5) {
    /* <30.5 days ~ 1 month */
    return floor($ts / 60 * 60 * 24 * 7) . " weeks ago";
    } elseif ($ts < 60 * 60 * 24 * 365) {
    /* <365 days = 1 year */
    return floor($ts / 60 * 60 * 24 * 30.5) . " months ago";
    } else {
    /* more than 1 year */
    return floor($ts / 60 * 60 * 24 * 365) . " years ago";
    }
    >
    }
    >
    Everything seems to work okay, except the "hours ago" case. A time 2
    hours ago will output "9692 hours ago", or a time 18 hours ago will
    read "9692 hours ago"!
    >
    Anyone help here? Thanks in advance
    It should be $ts / (60 * 60). Read about Operator Precedence:

    <http://www.php.net/manual/en/
    language.operat ors.php#languag e.operators.pre cedence>

    Comment

    • Rami Elomaa

      #3
      Re: Help me fix my &quot;x minutes ago&quot; time script please :)

      JamesG kirjoitti:
      I'm trying to convert a unix timestamp to a readable time, such as 4
      hours ago, or 25 minutes ago.
      My code is below:
      >
      function ago($ts) {
      /* time difference */
      $ts = (time() - $ts);
      >
      if($ts < 60) {
      /* <1 minute */
      return $ts . " seconds ago";
      } elseif($ts < 60 * 60) {
      /* <1 hour */
      return floor($ts / 60) . " minutes ago";
      } elseif ($ts < 60 * 60 * 2) {
      /* <2 hour */
      return "1 hour ago";
      } elseif ($ts < 60 * 60 * 24) {
      /* <24 hours = 1 day */
      return floor($ts / 60 * 60) . " hours ago";
      Parenthesis missing here. First you divide by 60, then multiply by 60,
      resulting in the same number you started with. This is the same as $ts *
      60 / 60 which is the same as $ts. You're getting seconds instead of
      hours. Put some parenthesis there to help php understund what you mean:
      $ts / (60 * 60) // now it's executed in the correct order
      } elseif ($ts < 60 * 60 * 24 * 2) {
      /* <2 days */
      return "1 day ago";
      } elseif ($ts < 60 * 60 * 24 * 7) {
      /* <7 days = 1 week */
      return floor($ts / 60 * 60 * 24) . " days ago";
      Same here: (60 * 60 * 24)
      } elseif ($ts < 60 * 60 * 24 * 30.5) {
      /* <30.5 days ~ 1 month */
      return floor($ts / 60 * 60 * 24 * 7) . " weeks ago";
      and here: (60 * 60 * 24 * 7)
      } elseif ($ts < 60 * 60 * 24 * 365) {
      /* <365 days = 1 year */
      return floor($ts / 60 * 60 * 24 * 30.5) . " months ago";
      and here
      } else {
      /* more than 1 year */
      return floor($ts / 60 * 60 * 24 * 365) . " years ago";
      .... and also here: (60 * 60 * 24 * 365)
      }
      }
      >
      Everything seems to work okay, except the "hours ago" case. A time 2
      hours ago will output "9692 hours ago", or a time 18 hours ago will
      read "9692 hours ago"!
      If it seemd to work okay then perhaps you hadn't tested the last ones,
      cos they seem to suffer from the same problem as the "x hours" case.

      A personal sidenote: I *hate* "elseif", it's so ASP/VB/M$! The one and
      only correct syntax is "else if" with a space between, if you ask me! :D
      (Sure, elseif *is* valid code but it's just disgusting!)

      --
      Rami.Elomaa@gma il.com

      "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
      usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

      Comment

      • JamesG

        #4
        Re: Help me fix my &quot;x minutes ago&quot; time script please :)

        On Apr 17, 7:37 pm, Rami Elomaa <rami.elo...@gm ail.comwrote:
        JamesG kirjoitti:
        >
        >
        >
        I'm trying to convert a unix timestamp to a readable time, such as 4
        hours ago, or 25 minutes ago.
        My code is below:
        >
        function ago($ts) {
        /* time difference */
        $ts = (time() - $ts);
        >
        if($ts < 60) {
        /* <1 minute */
        return $ts . " seconds ago";
        } elseif($ts < 60 * 60) {
        /* <1 hour */
        return floor($ts / 60) . " minutes ago";
        } elseif ($ts < 60 * 60 * 2) {
        /* <2 hour */
        return "1 hour ago";
        } elseif ($ts < 60 * 60 * 24) {
        /* <24 hours = 1 day */
        return floor($ts / 60 * 60) . " hours ago";
        >
        Parenthesis missing here. First you divide by 60, then multiply by 60,
        resulting in the same number you started with. This is the same as $ts *
        60 / 60 which is the same as $ts. You're getting seconds instead of
        hours. Put some parenthesis there to help php understund what you mean:
        $ts / (60 * 60) // now it's executed in the correct order
        >
        } elseif ($ts < 60 * 60 * 24 * 2) {
        /* <2 days */
        return "1 day ago";
        } elseif ($ts < 60 * 60 * 24 * 7) {
        /* <7 days = 1 week */
        return floor($ts / 60 * 60 * 24) . " days ago";
        >
        Same here: (60 * 60 * 24)
        >
        } elseif ($ts < 60 * 60 * 24 * 30.5) {
        /* <30.5 days ~ 1 month */
        return floor($ts / 60 * 60 * 24 * 7) . " weeks ago";
        >
        and here: (60 * 60 * 24 * 7)
        >
        } elseif ($ts < 60 * 60 * 24 * 365) {
        /* <365 days = 1 year */
        return floor($ts / 60 * 60 * 24 * 30.5) . " months ago";
        >
        and here
        >
        } else {
        /* more than 1 year */
        return floor($ts / 60 * 60 * 24 * 365) . " years ago";
        >
        ... and also here: (60 * 60 * 24 * 365)
        >
        }
        }
        >
        Everything seems to work okay, except the "hours ago" case. A time 2
        hours ago will output "9692 hours ago", or a time 18 hours ago will
        read "9692 hours ago"!
        >
        If it seemd to work okay then perhaps you hadn't tested the last ones,
        cos they seem to suffer from the same problem as the "x hours" case.
        >
        A personal sidenote: I *hate* "elseif", it's so ASP/VB/M$! The one and
        only correct syntax is "else if" with a space between, if you ask me! :D
        (Sure, elseif *is* valid code but it's just disgusting!)
        >
        --
        Rami.Elo...@gma il.com
        >
        "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
        usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze
        Thanks so much for correcting the silly mistake.
        I also hate nested elseifs but in this case it's the only way to do it
        I think-- I considered a switch statement but that isn't useful in
        this instance.



        Comment

        • Rami Elomaa

          #5
          Re: Help me fix my &quot;x minutes ago&quot; time script please :)

          JamesG kirjoitti:
          I also hate nested elseifs but in this case it's the only way to do it
          You kinda missed my point. I have nothing against else if's as they are,
          just the way of writing elseif together, without a space between them
          looks so stupid. As in "elseif" instead of "else if"... But that was
          really not important at all... :)

          --
          Rami.Elomaa@gma il.com

          "Wikipedia on vähän niinq internetin raamattu, kukaan ei pohjimmiltaan
          usko siihen ja kukaan ei tiedä mikä pitää paikkansa." -- z00ze

          Comment

          • gosha bine

            #6
            Re: Help me fix my &quot;x minutes ago&quot; time script please :)

            JamesG wrote:
            >
            Thanks so much for correcting the silly mistake.
            I also hate nested elseifs but in this case it's the only way to do it
            I think-- I considered a switch statement but that isn't useful in
            this instance.
            >
            >
            >
            you actually don't need else's here, because you return in every branch.
            Just use ifs:

            if($ts < 60)
            return $ts . " seconds ago";
            if($ts < (60 * 60))
            return intval($ts / 60) . " minutes ago";

            etc


            --
            gosha bine

            extended php parser ~ http://code.google.com/p/pihipi
            blok ~ http://www.tagarga.com/blok

            Comment

            • Toby A Inkster

              #7
              Re: Help me fix my &quot;x minutes ago&quot; time script please :)

              Rami Elomaa wrote:
              A personal sidenote: I *hate* "elseif", it's so ASP/VB/M$! The one and
              only correct syntax is "else if" with a space between, if you ask me! :D
              (Sure, elseif *is* valid code but it's just disgusting!)
              I often use "elseif" -- before I started using PHP my main scripting
              language was Perl, which has "elsif", so switching to "elseif" was only a
              small learning curve.

              --
              Toby A Inkster BSc (Hons) ARCS
              Fast withdrawal casino UK 2025 – Play now & cash out instantly! Discover the top sites for rapid, secure payouts with no delays.

              Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

              * = I'm getting there!

              Comment

              Working...