3v4l.org

run code in 500+ PHP versions simultaneously
<?php class Tuvi { private $day; private $month; private $year; const CAN = ["Giáp", "Ất", "Bính", "Đinh", "Mậu", "Kỷ", "Canh", "Tân", "Nhâm", "Qúy"]; const CHI = ["Tí", "Sửu", "Dần", "Mão", "Thìn", "Tỵ", "Ngọ", "Mùi", "Thân", "Dậu", "Tuất", "Hợi"]; public function __construct(int $day, int $month, int $year) { $this->day = $day; $this->month = $month; $this->year = $year; } public function toLunarDate() : string { $can = 3; $chi = 9; $comparingDate = new self(1, 3, 1996); $diffDays = $this->diffDays($comparingDate); if ($this->isLessThan($comparingDate)) { $can = ($can - $diffDays % 10 + 10) % 10; $chi = ($chi - $diffDays % 12 + 12) % 12; } else if ($this->isGreaterThan($comparingDate)) { $can = ($can + $diffDays % 10) % 10; $chi = ($chi + $diffDays % 12) % 12; } return sprintf("%s %s", static::CAN[$can], static::CHI[$chi]); } public function diffDays(Tuvi $t) : int { return $this->toDateTime()->diff($t->toDateTime())->days; } public function isLessThan(Tuvi $t) : bool { return $this->toDateTime() < $t->toDateTime(); } public function equals(Tuvi $t) : bool { return $this->toDateTime() == $t->toDateTime(); } public function isGreaterThan(TuVi $t) : bool { return $this->toDateTime() > $t->toDateTime(); } public function toDateTime() : DateTime { return new DateTime("{$this->year}-{$this->month}-{$this->day}"); } } $x = new Tuvi(31, 1, 1988); $y = new Tuvi(31,12,1989); var_dump( $x->diffDays($y), $x->isLessThan($y), $y->isLessThan($x), $x->toLunarDate(), (new Tuvi(15,12,1990))->toLunarDate(), (new Tuvi(25,3,2000))->toLunarDate(), (new Tuvi(27,11,1995))->toLunarDate() );

preferences:
57.5 ms | 1503 KiB | 5 Q