Skip to content

Commit

Permalink
using php DateTime diff
Browse files Browse the repository at this point in the history
  • Loading branch information
zaliqarosli committed Jan 22, 2020
1 parent 0b76f9c commit 30c741e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
5 changes: 4 additions & 1 deletion modules/timepoint_list/php/timepoint_list.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ class Timepoint_List extends \NDB_Menu
$this->tpl_data['candID'] = $this->candID;
$this->tpl_data['PSCID'] = $candidate->getPSCID();
$listOfTimePoints = $candidate->getListOfTimePoints();
$this->tpl_data['candidate'] = $candidate->getData();
$this->tpl_data['candidate'] = array_merge(
$candidate->getData(),
array('Age' => $candidate->getAgeInMonths())
);

if (!empty($listOfTimePoints)) {
$user =& \User::singleton();
Expand Down
6 changes: 6 additions & 0 deletions modules/timepoint_list/templates/menu_timepoint_list.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
<th>
DOB
</th>
<th>
Age
</th>
{if $candidate.EDC!=""}
<th>
EDC
Expand All @@ -30,6 +33,9 @@
<td>
{$candidate.DoB}
</td>
<td>
{$candidate.Age}
</td>
{if $candidate.EDC!=""}
<td>
{$candidate.EDC}
Expand Down
22 changes: 13 additions & 9 deletions php/libraries/Candidate.class.inc
Original file line number Diff line number Diff line change
Expand Up @@ -664,9 +664,12 @@ class Candidate
*/
public function getAge(): array
{
return \Utility::calculateAge(
$this->candidateInfo["DoB"],
(new DateTime())->format('Y-m-d')
$dob = new DateTime($this->candidateInfo["DoB"]);
$age = $dob->diff(new DateTime('now'))->format('%y,%m,%d');

return array_combine(
array('year', 'mon', 'day'),
explode(",", $age)
);
}

Expand All @@ -677,7 +680,9 @@ class Candidate
*/
public function getAgeYears(): int
{
return $this->getAge()['year'];
$dob = new DateTime($this->candidateInfo["DoB"]);

return (int)$dob->diff(new DateTime('now'))->format('%y');
}

/**
Expand All @@ -687,10 +692,9 @@ class Candidate
*/
public function getAgeInMonths(): int
{
$age = $this->getAge();
$months = $age['year']*12 + $age['mon'] + ($age['day']/30);
$dob = new DateTime($this->candidateInfo["DoB"]);

return (round($months*10) / 10.0);
return (int)$dob->diff(new DateTime('now'))->format('%m') + 12 * $this->getAgeYears();
}

/**
Expand All @@ -700,9 +704,9 @@ class Candidate
*/
public function getAgeInDays(): int
{
$age = $this->getAge();
$dob = new DateTime($this->candidateInfo["DoB"]);

return $age['year']*365 + $age['mon']*30 + $age['day'];
return (int)$dob->diff(new DateTime('now'))->format('%a');
}

/**
Expand Down

0 comments on commit 30c741e

Please sign in to comment.