Gistrec

Convert second to word [RU][PHP]

Aug 26th, 2016
389
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.05 KB | None | 0 0
  1. <?php
  2. /*
  3. * Функция ConvertSecToWord($sec) позволяет получить
  4. * лексически-грамотные предложения о оставшемся времени.
  5. * Пример: ConvertSecToWord(123456789) выведет:
  6. * 3 года 11 месяцев 3 дня 21 час 33 минуты 9 секунд
  7. * !!!Внимание, обратите внимание на $this && public (ООП)
  8. */
  9.  
  10.  public function ConvertSecToWord($sec){
  11.      $mes = '';
  12.      $SecOnYear   = 31536000;
  13.      $SecOnMonth  = 2592000;
  14.      $SecOnWeek   = 604800;
  15.      $SecOnDay    = 86400;
  16.      $SecOnHour   = 3600;
  17.      $SecOnMin    = 60;
  18.      if (intval($sec / $SecOnYear) > 0){
  19.          $year = intval($sec / $SecOnYear);
  20.          $sec  = $sec - ($year * $SecOnYear);
  21.          $mes  = $this->CorrectString($year,'year');
  22.      }
  23.      if (intval($sec / $SecOnMonth) > 0){
  24.          $month = intval($sec / $SecOnMonth);
  25.          $sec   = $sec - ($month * $SecOnMonth);
  26.          $mes  .= $this->CorrectString($month,'month');
  27.      }
  28.      if (intval($sec / $SecOnWeek) > 0){
  29.          $week = intval($sec / $SecOnWeek);
  30.          $sec  = $sec - ($week * $SecOnWeek);
  31.          $mes .= $this->CorrectString($week,'week');
  32.      }
  33.      if (intval($sec / $SecOnDay) > 0){
  34.          $day  = intval($sec / $SecOnDay);
  35.          $sec  = $sec - ($day * $SecOnDay);
  36.          $mes .= $this->CorrectString($day,'day');
  37.      }
  38.      if (intval($sec / $SecOnHour) > 0){
  39.          $hour = intval($sec / $SecOnHour);
  40.          $sec  = $sec - ($hour * $SecOnHour);
  41.          $mes .= $this->CorrectString($hour,'hour');
  42.      }
  43.      if (intval($sec / $SecOnMin) > 0){
  44.          $min = intval($sec / $SecOnMin);
  45.          $sec  = $sec - ($min * $SecOnMin);
  46.          $mes .= $this->CorrectString($min,'min');
  47.      }
  48.  
  49.      if ($sec > 0) $mes .= $this->CorrectString($sec,'sec');
  50.      return $mes;    
  51.  }
  52.  
  53.  public function CorrectString($number, $type){
  54.      $word = array('year'  => array('год'    ,'года'    ,'лет'   ),
  55.                    'month' => array('месяц'  ,'месяца' ,'месяцев'),
  56.                    'week'  => array('неделя' ,'недели' ,'недель' ),
  57.                    'day'   => array('день'   ,'дня'    ,'дней'   ),
  58.                    'hour'  => array('час'    ,'часа'   ,'часов'  ),
  59.                    'min'   => array('минута' ,'минуты' ,'минут'  ),
  60.                    'sec'   => array('секунда','секунды','секунд' ));
  61.      $number = (string) $number;
  62.      if (strlen($number) > 1){
  63.          if ($number[strlen($number)-2].$number[strlen($number)-1] == '11'){
  64.              $LastNum = '10';
  65.          }else{
  66.              $LastNum = $number[strlen($number)-1];
  67.          }
  68.      }else{
  69.          $LastNum = $number[strlen($number)-1];
  70.      }
  71.      if ($LastNum == 1){
  72.          $str = 0;
  73.      }elseif($LastNum == 2 || $LastNum == 3 || $LastNum == 4){
  74.          $str = 1;
  75.      }else{
  76.          $str = 2;
  77.      }
  78.      return ($number.' '.$word[$type][$str].' ');
  79.  }
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment