GetText的相对时间

时间:2012-04-01 11:14:27

标签: php time gettext

我正在使用php5和gettext开发多语言应用程序。

Rightnow我想显示一个相对时间但是我遇到了一些问题,同时保留了多语言。

我尝试谷歌谷歌寻求解决方案,但找不到合适的方法来做到这一点,并且无法让我自己的改变工作。

我希望你有一个解决方案。

2 个答案:

答案 0 :(得分: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))
                                                 ));
}

?>

这是我的解决方案。

答案 1 :(得分:0)

如果您对使用客户端实施没问题,可能需要查看http://timeago.yarp.com/ 它取消了服务器端负载,提供了更好的缓存选项,并具有多语言支持。