PHP를 사용한 ISO8601duration 일본어

4566 단어 PHP
PHP로 웹 API를 수행할 때 반응 중PT1H1M1S인 두아르션으로 답장할 때 일본어화된 호각반을 원해서 써봤어요.
ISO8601 duration specification에 관해서는 P2Y3DT6H8M!?이 암호를 해석하기 위한 ISO8601 duration specification이란가 상세하므로 참조
참조: PHP: DateInterval::format - Manual
class Iso8601Formatter
{
  public $val;
  public function __construct($duration){
    $this->val = $duration;
  }

  function toJp()
  {
    $interval = new \DateInterval($this->val);
    $fmt = "";
    if($interval->h > 0) {
      $fmt.=('%h時間');
    }
    if($interval->i > 0){
      $fmt.=('%i分');
    }
    if($interval->s > 0){
      $fmt.=('%s秒');
    }
    return $interval->format($fmt);
  }
}

echo join(PHP_EOL,
  [
    (new Iso8601Formatter('PT1H1M1S'))->toJp(),
    (new Iso8601Formatter('PT14M10S'))->toJp(),
    (new Iso8601Formatter('PT2H9M'))->toJp(),
    (new Iso8601Formatter('PT2H59S'))->toJp()
  ]);
실행 결과
1時間1分1秒
14分10秒
2時間9分
2時間59秒   

좋은 웹페이지 즐겨찾기