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 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';
    

    }

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog