[PHP + AI] CHATGPT로 만든 생일, 기념일 계산 함수

function calculate_anniversaries($birthday, $debut)
{
      // Get current date
      $now = new DateTime();

      // Convert birthday to DateTime object
      $birthday = DateTime::createFromFormat('md', $birthday);
      $birthday->setDate(intval($now->format('Y')), $birthday->format('m'), $birthday->format('d'));

      // Check if birthday has already passed in current year
      if ($now > $birthday) {
            $birthday->modify('+1 year');
      }

      // Calculate days until next birthday
      $days_to_next_birthday = ($now->diff($birthday)->days) % 365;

      // Convert debut to DateTime object
      $debut = new DateTime($debut);

      // Calculate days since debut
      $days_since_debut = $now->diff($debut)->days;

      // Calculate the nearest debut anniversary (in days)
      $nearest_debut_anniversary = ($days_since_debut % 365) ? 365 - ($days_since_debut % 365) : 0;

      // Calculate the year of the nearest debut anniversary
      $nearest_debut_anniversary_year = intval($days_since_debut / 365) + ($nearest_debut_anniversary === 0 ? 0 : 1);

      // Calculate the nearest 100-day anniversary (in days)
      $nearest_100_day_anniversary = ($days_since_debut % 100) ? 100 - ($days_since_debut % 100) : 0;

      // Calculate the number of the nearest 100-day anniversary
      $nearest_100_day_anniversary_number = intval($days_since_debut / 100) + 1;

      // Return all the results
      return [
            "생일 D-DAY" => $days_to_next_birthday,
            "방송 X일째" => $days_since_debut + 1,
            "주년 D-DAY" => $nearest_debut_anniversary,
            "몇주년" => $nearest_debut_anniversary_year,
            "몇백일 D-DAY" => $nearest_100_day_anniversary,
            "몇백일" => $nearest_100_day_anniversary_number * 100
      ];
}
위 함수는 아래의 명령으로 CHATGPT에 넣은 후, 일부 가공 및 수정한 결과 입니다.

PHP로 함수를 만들어 줘.

1. 입력값은 두가지야. 첫번째는 월과 일로 이루어진 4자리 숫자의 생일이야, 예를 들면 생일이 1월 5일이면 "0105" 를 입력할거야. 그리고 두번째는 데뷔일인데 형식은 "2000-01-01 00:00:00" 으로 입력할거야.
2. 출력은 다음 생일이 오늘로부터 며칠 남았는지, 데뷔일로부터 며칠 지났는지, 오늘로부터 제일 가까운 데뷔일 X주년 기념일이 오늘로부터 며칠 남았는지, 오늘로부터 제일 가까운 데뷔일 X주년 기념일이 몇주년 기념일인지, 데뷔일에서 제일 가까운 몇백일 기념일이 오늘로부터 며칠 남았는지, 제일 가까운 몇백일 기념일이 몇백일 기념일인지, 모두 계산해서 반환해 줘.
3. 모든 반환값의 단위는 일(day)로, 정수로 변환해서 반환해 줘.
4. 모든 반환값 배열의 키는 한글로 부탁해.
5. 데뷔일 기념일 계산할 때, 데뷔 당일을 1일째로 계산해 줘.
6. 답변은 20줄씩 나눠서 대답해 줘.

위 함수를 추가 개선한 함수 입니다. (20230222)

function calculate_anniversaries($birthday, $debut) {
      $returnArr = [
            "birth_dDay" => null, // 생일 D-Day
            "debut_dDay" => null, // 방송 X일째
            "anniver_dDay" => null, // 주년 D-Day            
            "what_year_anniver" => null, // 몇주년      
            "d100_dDay" => null, // 몇백일 D-Day      
            "what_d100_day_anniver" => null, // 몇백일
            "anniver_date" => null // 주년 or 몇백일 날짜
      ];

      // 현재 자정 시간
      $now = new DateTime("midnight");

      // 생일 디데이 계산
      if($birthday !== "0000"){
            // Convert birthday to DateTime object
            $birthday = DateTime::createFromFormat('md', $birthday);
            $birthday->setDate(intval($now->format('Y')), $birthday->format('m'), $birthday->format('d'));

            // Check if birthday has already passed in current year
            if ($now > $birthday) {
                  $birthday->modify('+1 year');
            }

            // Calculate days until next birthday
            $returnArr["birth_dDay"] = ($now->diff($birthday)->days) % 365;
      }

      // 데뷔 이벤트 계산
      if($debut !== "2000-01-01 00:00:00"){
            // Convert debut to DateTime object
            $debut = new DateTime($debut);

            // Calculate days since debut
            // $days_since_debut = $now->diff($debut)->days;
            $days_since_debut = (int)$debut->diff($now)->format('%r%a');

            if($days_since_debut < 0){
                  // 데뷔 전
                  $returnArr["debut_dDay"] = $days_since_debut;
                  $returnArr["anniver_date"] = (new DateTime("midnight"))->modify('+'.($days_since_debut*-1).' day')->format('n월 d일');
            }else{
                  // 데뷔 후 (데뷔일 포함)

                  // Calculate the nearest debut anniversary (in days)
                  $nearest_debut_anniversary = ($days_since_debut % 365) ? 365 - ($days_since_debut % 365) : 0;

                  // Calculate the year of the nearest debut anniversary
                  $nearest_debut_anniversary_year = intval($days_since_debut / 365) + ($nearest_debut_anniversary === 0 ? 0 : 1);

                  // Calculate the nearest 100-day anniversary (in days)
                  $nearest_100_day_anniversary = 100 - (($days_since_debut + 1) % 100);
                  if($nearest_100_day_anniversary >= 100){
                        $nearest_100_day_anniversary -= 100;
                  }

                  // Calculate the number of the nearest 100-day anniversary
                  $nearest_100_day_anniversary_number = intval($days_since_debut / 100) + 1;

                  $returnArr["debut_dDay"] = $days_since_debut + 1;
                  $returnArr["anniver_dDay"] = $nearest_debut_anniversary;
                  $returnArr["what_year_anniver"] = $nearest_debut_anniversary_year;
                  $returnArr["d100_dDay"] = $nearest_100_day_anniversary;
                  $returnArr["what_d100_day_anniver"] = $nearest_100_day_anniversary_number * 100;

                  $minimum_evt_dDay = 0;
                  if($nearest_debut_anniversary < $nearest_100_day_anniversary){
                        $minimum_evt_dDay = $nearest_debut_anniversary;                        
                  }else{
                        $minimum_evt_dDay = $nearest_100_day_anniversary;
                  }
                  $returnArr["anniver_date"] = (new DateTime("midnight"))->modify('+'.$minimum_evt_dDay.' day')->format('n월 d일');
            }
      }

      return $returnArr;
}

Leave a Comment