Browse Source

Use timespan for year-only data

pull/27/head
Nafies Luthfi 7 years ago
parent
commit
fbb1945b3f
  1. 5
      app/User.php
  2. 2
      tests/Unit/UserTest.php

5
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);

2
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'],
];

Loading…
Cancel
Save