개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.07.27 00:20

IntlDateFormatter::formatObject 관련 질문

목록
  • 개발하는곰돌이 1일 전 2025.07.27 00:20
  • 12
    1
제가 IntlDateFormatter를 사용하여 날짜를 포맷하는 중에 문제가 생겼습니다.
IntlDateFormatter::formatObject 메소드를 사용하여 날짜 객체를 포맷할 때, 날짜 객체가 null일 때 예외가 발생하는 것을 확인했습니다.

IntlDateFormatter::formatObject 메소드에서 null 날짜 객체를 처리하는 방법을 알려주세요?

예를 들어, 다음 코드는 null 날짜 객체를 처리하는 방법을 알려주세요.

php

$date = null;

$formatter = new IntlDateFormatter('ko_KR', IntlDateFormatter::FULL, IntlDateFormatter::FULL);

try {

    $formattedDate = $formatter->formatObject($date);

} catch (Exception $e) {

    echo '날짜 객체가 null입니다.';

}


    댓글목록

    profile_image
    나우호스팅  1일 전



    IntlDateFormatter::formatObject 메소드는 null 날짜 객체를 처리할 때 NullPointerException이 발생합니다.

    null 날짜 객체를 처리할 때는 try-catch 블록을 사용하여 예외를 잡아 날짜 객체가 null인 경우 대체 메시지를 출력할 수 있습니다.

    예를 들어, 다음 코드는 null 날짜 객체를 처리하는 방법을 보여줍니다.

    #hostingforum.kr
    php
    
    $date = null;
    
    $formatter = new IntlDateFormatter('ko_KR', IntlDateFormatter::FULL, IntlDateFormatter::FULL);
    
    try {
    
        $formattedDate = $formatter->formatObject($date);
    
    } catch (Exception $e) {
    
        echo '날짜 객체가 null입니다.';
    
    } else {
    
        echo $formattedDate;
    
    }
    
    


    또는 null 날짜 객체를 처리할 때는 null 체크를 사용하여 날짜 객체가 null인지 확인할 수 있습니다.

    #hostingforum.kr
    php
    
    $date = null;
    
    $formatter = new IntlDateFormatter('ko_KR', IntlDateFormatter::FULL, IntlDateFormatter::FULL);
    
    if ($date !== null) {
    
        $formattedDate = $formatter->formatObject($date);
    
        echo $formattedDate;
    
    } else {
    
        echo '날짜 객체가 null입니다.';
    
    }
    
    


    두 코드 모두 null 날짜 객체를 처리할 때 예외를 잡거나 null 체크를 사용하여 날짜 객체가 null인지 확인할 수 있습니다.

    2025-07-27 00:21

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 39,424건 / 1 페이지

검색

게시물 검색