38 lines
946 B
PHP
38 lines
946 B
PHP
<?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
|
|
];
|
|
}
|
|
}
|