douzi2333
2017-03-08 10:06 阅读 33
已采纳

更改为ex hour to hours时,已过去的日期时间更改名称字符串

I've got this code snippet as you can see my language is a bit different then just adding the 'S the part where Guess it's something here? is where the s did go when i found this snippet, so what would be a good approach I've got stuck on ides at this moment.

    $string = array(
    'y' => 'år',
    'm' => 'månad',  // månader
    'w' => 'vecka',  // veckor
    'd' => 'dag',    // dagar
    'h' => 'timme',  // timmar
    'i' => 'minut',  // minuter
    's' => 'sekund', // sekunder
);
foreach ($string as $k => &$v) {
    if ($diff->$k) {
        $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 'Guess it's somthing here ?' : '');
    } else {
        unset($string[$k]);
    }
}

FULL CODE HERE

function time_elapsed_string($datetime, $level = 7) {

$now = new DateTime;
$ago = new DateTime($datetime);
$diff = $now->diff($ago);

$diff->w = floor($diff->d / 7);
$diff->d -= $diff->w * 7;

$string = array(
    'y' => 'år',
    'm' => 'månader',
    'w' => 'veckor',
    'd' => 'dagar',
    'h' => 'timme',
    'i' => 'minuter',
    's' => 'sekunder',
);
foreach ($string as $k => &$v) {
    if ($diff->$k) {
        $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
    } else {
        unset($string[$k]);
    }
}

$string = array_slice($string, 0, $level);
return $string ? implode(', ', $string) . ' ago' : 'just now';

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douchen2025 douchen2025 2017-03-08 11:28

    I've got it to work also did improve it to use future date. Here is the code if anyone feel the need for it if you're going to use it on a non english site with alot of bendings in it.

    instead of full date you can change to time_elapsed_string(date,2) = year month.

    function time_elapsed_string($datetime, $level = 7) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);
    
    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;
    
    $string = array(
        'y' => 'år',           // year
        'm' => 'månad',        // month
        'w' => 'vecka',        // week
        'd' => 'dag',          // day
        'h' => 'timme',        // hour
        'i' => 'minut',        // minute
        's' => 'sekund',       // second
    );
    $string2 = array(
        'y' => 'år',           // year
        'm' => 'månader',      // months
        'w' => 'veckor',       // weeks
        'd' => 'dagar',        // days
        'h' => 'timmar',       // hours
        'i' => 'minuter',      // minutes
        's' => 'sekunder',     // seconds
    );
    
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' .  ($v =$diff->$k > 1 ? $string2[$k] : '');
        } else {
            unset($string[$k]);
        }
    }
    // ago, to, just now
    string = array_slice($string, 0, $level);
    return $string ? implode(', ', $string) . ($diff->invert ? ' sen' : ' tills') : ' precis nu';
    

    }

    点赞 评论 复制链接分享

相关推荐