php zaman farkı komut dosyası sorunu

2 Cevap

İşte benim php kodu .....

<?php
function nicetime($date)
{
    if(empty($date)) {
        return "No date provided";
    }

    $periods         = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths         = array("60","60","24","7","4.35","12","10");

    $now             = time();
    $unix_date  = strtotime($date);

       // check validity of date
    if(empty($unix_date)) {    
        return "Incorrect Date";
    }

    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";

    } else {
        $difference     = $unix_date - $now;
        $tense         = "ago";
    }

    for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
        $difference /= $lengths[$j];
    }

    $difference = round($difference);

    if($difference != 1) {
        $periods[$j].= "s";
    }

    return "$difference $periods[$j] {$tense}";
}
?>

ancak doğru zamanı gösteren değil .... ve herhangi bir geçerli zaman kendisine verilirse böylece 25-35 saniye önce gösteren ve .... ama bir de digg.com gibi bir zaman farkı funtion yapmak istiyorum 2 Nane VE 3 Sec benzeri format önce ... ben bunu nasıl yapabilirim

2 Cevap

Try it to do with the other way - at the beginning multiply all $lengths values (i.e. $multiplied).
1. Divide $difference with that value.
2. If >= 1 then you have a solution.
Else divide $multiplied with the array_pop($lengths) value. Go to 2.
3. You have the solution.

Ben o olacağını düşünüyorum.

Btw - $tense her zaman "ago".

Bir göz atın ve Date::span ve Kohana Date class ve Date::fuzzy_span yöntemleri öğrenirler.