diff --git a/app/User.php b/app/User.php index 1205e80..5cf5b62 100644 --- a/app/User.php +++ b/app/User.php @@ -235,18 +235,19 @@ class User extends Authenticatable public function getAgeAttribute() { $age = null; + $yearOnlySuffix = Carbon::now()->format('-m-d'); if ($this->dob && !$this->dod) { $age = Carbon::parse($this->dob)->timespan(); } if (!$this->dob && $this->yob) { - $age = (date('Y') - $this->yob).' tahun'; + $age = Carbon::parse($this->yob.$yearOnlySuffix)->timespan(); } if ($this->dob && $this->dod) { $age = Carbon::parse($this->dob)->timespan($this->dod); } if (!$this->dob && $this->yob && !$this->dod && $this->yod) { - $age = ($this->yod - $this->yob).' tahun'; + $age = Carbon::parse($this->yob.$yearOnlySuffix)->timespan($this->yod.$yearOnlySuffix); } if ($this->dob && $this->yob && $this->dod && $this->yod) { $age = Carbon::parse($this->dob)->timespan($this->dod); diff --git a/tests/Unit/UserTest.php b/tests/Unit/UserTest.php index bfa8286..212bb7b 100644 --- a/tests/Unit/UserTest.php +++ b/tests/Unit/UserTest.php @@ -174,7 +174,7 @@ class UserTest extends TestCase return [ ['2018-02-02', '1997-01-01', '1997', null, null, '21 tahun, 1 bulan, 1 hari'], ['2018-02-02', '1997-01-01', null, null, null, '21 tahun, 1 bulan, 1 hari'], - ['2018-02-02', null, '1997', null, null, '22 tahun'], + ['2018-02-02', null, '1997', null, null, '21 tahun'], ['2018-02-02', '1997-01-01', '1997', '2017-01-01', '2017', '20 tahun'], ['2018-02-02', null, '1997', null, '2017', '20 tahun'], ];