PHP:排除一个单词

时间:2018-02-06 16:21:21

标签: php time internationalization

我想从下面的代码中排除“ mois ”这个词,以防止在其中添加“s”。

function ago($time) {
   $periods = array("seconde", "minute", "heure", "jour", "semaine", "mois", "année", "décade");
   $lengths = array("60","60","24","7","4.35","12","10");

   $now = time();

       $difference     = $now - $time;
       $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 "Il y a $difference $periods[$j].";
}

1 个答案:

答案 0 :(得分:3)

使用substr($periods[$j], -1),您将获得字符串中的最后一个字符,因此

if($difference != 1 && substr($periods[$j], -1) != 's') {
    $periods[$j].= "s";
}