0

i'm working on a multilanguage application with php5 and gettext.

Rightnow i would like to show a relative time but i've got some problems with it, while keeping the multilanguage.

I tried google to google for a solution but could not find a proper way to do it and could not get the changes to work on my own.

I hope you've got a solution for this.

Aurelio De Rosa
  • 21,856
  • 8
  • 48
  • 71
Frederick Behrends
  • 3,075
  • 23
  • 47
  • What do you mean with relative time? – Aurelio De Rosa Apr 01 '12 at 11:29
  • Probably "5 minutes ago", etc. http://stackoverflow.com/questions/18685/how-to-display-12-minutes-ago-etc-in-a-php-webpage Would you care to explain what problem you are having? – Basti Apr 01 '12 at 11:39
  • One common way is `printf(_('%d seconds ago'), $seconds)`. That way, the translations just need to know to have the conversion specification present without caring about the actual value. – salathe Apr 01 '12 at 12:23

2 Answers2

0
<?php

function getRelativeTime($date) {
    $diff = time() - strtotime($date);
    if ($diff<60)
        return strtr(ngettext("vor einer Sekunde", "vor %time% Sekunden", $diff), array("%time%" => $diff));
    $diff = round($diff/60);
    if ($diff<60)
        return strtr(ngettext("vor einer Minute", "vor %time% Minuten", $diff), array("%time%" => $diff));
    $diff = round($diff/60);
    if ($diff<24)
        return strtr(ngettext("vor einer Stunde", "vor %time% Stunden", $diff), array("%time%" => $diff));
    $diff = round($diff/24);
    if ($diff<7)
        return strtr(ngettext("vor einem Tag", "vor %time% Tagen", $diff), array("%time%" => $diff));
    $diff = round($diff/7);
    if ($diff<4)
        return strtr(ngettext("vor einer Woche", "vor %time% Wochen", $diff), array("%time%" => $diff));
    $diff = round($diff/4);
    if ($diff<12)
        return strtr(ngettext("vor einem Monat", "vor %time% Monaten", $diff), array("%time%" => $diff));
    return strtr(_("am %date% um %time%"), array("%time%" => strftime('%X', strtotime($date)),
                                                 "%date%" => strftime('%x', strtotime($date))
                                                 ));
}

?>

This is my solution.

Frederick Behrends
  • 3,075
  • 23
  • 47
  • Could add a little bit more information, because it's kinda "hard" to understand your could, by simply reading it. Thank you very much ;) – Julius F Apr 03 '12 at 20:32
0

If you're okay with using a client-side implementation, you might want to check out http://timeago.yarp.com/ It takes off server side load, provides better options for caching, and has multilingual support.

Deepak Thomas
  • 3,355
  • 4
  • 37
  • 39