
DateTime::createFromInterface 함수는 DateTime 인스턴스를 생성하기 위해 인터페이스를 구현한 클래스의 인스턴스를 인자로 넘겨야 합니다.
인터페이스 인자는 DateTime::createFromInterface 함수에서 날짜와 시간을 생성하는 데 필요한 정보를 제공하는 역할을 합니다.
예를 들어, DateIntervalInterface 인터페이스를 구현한 클래스의 인스턴스를 넘겨서 날짜 간격을 생성할 수 있습니다.
DateIntervalInterface 인터페이스는 다음과 같은 메소드를 구현해야 합니다.
- format()
- getDays()
- getHours()
- getMinutes()
- getMonths()
- getSeconds()
- getSign()
- getYears()
이러한 메소드는 날짜 간격을 생성하고 관리하는 데 필요한 정보를 제공합니다.
DateIntervalInterface 인터페이스를 구현한 클래스의 인스턴스를 넘겨서 날짜 간격을 생성하는 예제는 다음과 같습니다.
#hostingforum.kr
php
interface DateIntervalInterface {
public function format();
public function getDays();
public function getHours();
public function getMinutes();
public function getMonths();
public function getSeconds();
public function getSign();
public function getYears();
}
class CustomDateInterval implements DateIntervalInterface {
private $days;
private $hours;
private $minutes;
private $months;
private $seconds;
private $sign;
private $years;
public function __construct($days, $hours, $minutes, $months, $seconds, $sign, $years) {
$this->days = $days;
$this->hours = $hours;
$this->minutes = $minutes;
$this->months = $months;
$this->seconds = $seconds;
$this->sign = $sign;
$this->years = $years;
}
public function format() {
// 날짜 간격을 문자열로 변환하는 로직
}
public function getDays() {
return $this->days;
}
public function getHours() {
return $this->hours;
}
public function getMinutes() {
return $this->minutes;
}
public function getMonths() {
return $this->months;
}
public function getSeconds() {
return $this->seconds;
}
public function getSign() {
return $this->sign;
}
public function getYears() {
return $this->years;
}
}
$dateInterval = new CustomDateInterval(1, 2, 3, 4, 5, 1, 6);
$date = DateTime::createFromInterface($dateInterval);
echo $date->format('Y-m-d H:i:s');
이 예제에서는 CustomDateInterval 클래스를 구현하여 DateIntervalInterface 인터페이스를 구현한 클래스의 인스턴스를 생성합니다. 이 인스턴스를 DateTime::createFromInterface 함수에 넘겨 날짜 간격을 생성하고 관리합니다.
2025-08-06 18:15