This commit is contained in:
37
api/app/Services/Geral/AgeService.php
Normal file
37
api/app/Services/Geral/AgeService.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Geral;
|
||||
|
||||
use DateTime;
|
||||
|
||||
class AgeService
|
||||
{
|
||||
public static function getFormattedAge(DateTime $dateOfBirth, DateTime $dateToCalc = null)
|
||||
{
|
||||
$age = AgeService::getAge($dateOfBirth, $dateToCalc);
|
||||
|
||||
$yearsText = $age->years > 1 ? 'anos' : 'ano';
|
||||
$monthsText = $age->months > 1 ? 'meses' : 'mês';
|
||||
$daysText = $age->days > 1 ? 'dias' : 'dia';
|
||||
|
||||
return "{$age->years} {$yearsText} e "
|
||||
. "{$age->months} {$monthsText} e "
|
||||
. "{$age->days} {$daysText}";
|
||||
}
|
||||
|
||||
public static function getAge(DateTime $dateOfBirth, DateTime $dateToCalc = null)
|
||||
{
|
||||
if (is_null($dateToCalc)) {
|
||||
$dateToCalc = new DateTime('today');
|
||||
}
|
||||
|
||||
|
||||
$interval = $dateOfBirth->diff($dateToCalc);
|
||||
|
||||
return (object) [
|
||||
'years' => $interval->y,
|
||||
'months' => $interval->m,
|
||||
'days' => $interval->d
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user