Smuks PHP datums

Kurš vispār izgudrojis datuma formātu? Parastam cilvēkam ir grūti saprast, piemēram, pirms cik dienām bija 12. jūlijs, tādēļ labāk ir datumu atspoguļot šādi: “pirms 2 dienām” vai “pēc 15 minūtēm”. Atradu tādu funkciju šeit: http://lv.php.net/time, bet tā bija angliski, tādēļ latviskoju to.

function nicetime($date)
{
    if(empty($date)) {
        return "nav norādīts datums";
    }
    $periods         = array("skeundes", "minūtes", "stundas", "dienas", "nedēļas", "mēneša", "gada", "desmitgades");
    $periods1         = array("skeundēm", "minūtēm", "stundām", "dienām", "nedēļām", "mēnešiem", "gadiem", "desmitgadēm");
    $lengths         = array("60","60","24","7","4.35","12","10");
    $now             = time();
    $unix_date         = strtotime($date);
    if(empty($unix_date)) {
        return "Nepareizs datums";
    }

    if($now > $unix_date) {
        $difference     = $now - $unix_date;
        $tense         = "pirms";
    } else {
        $difference     = $unix_date - $now;
        $tense         = "pēc";
    }
    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }
    $difference = round($difference);
    if($difference != 1) {
        $periods[$j] = $periods1[$j];
    }
    return "$tense $difference $periods[$j]";
}

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Comments »

 
 

Pievienot komentāru

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

*