Last active
November 27, 2020 10:23
-
-
Save nathandaly/4533994893bb6e8b1a22b2f0acc82aa5 to your computer and use it in GitHub Desktop.
Calculate Age in PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
namespace App; | |
use App\Models\Age; | |
use Carbon\Carbon; | |
class AgeCalculator | |
{ | |
public const FEB_28 = 59; | |
public const MAX_MONTHS = 12; | |
/** | |
* Demonstrate a more classic approach to age calculation. | |
* | |
* @param Carbon $dateOfBirth | |
* @return Age | |
*/ | |
public function __invoke(Carbon $dateOfBirth): array | |
{ | |
$currentDateTime = Carbon::now(); | |
// Indicates whether dob year has extra day compare to present date i.e. Leap years. | |
$extraDay = $dateOfBirth->isLeapYear() | |
&& !$currentDateTime->isLeapYear() | |
&& $dateOfBirth->dayOfYear > self::FEB_28 ? 1 : 0; | |
// Indicates whether dob has already been celebrated this year. | |
$hasDobOccurred = $currentDateTime->dayOfYear >= $dateOfBirth->dayOfYear - $extraDay; | |
$age = [ | |
'years' => ($currentDateTime->year - $dateOfBirth->year - ($hasDobOccurred ? 0 : 1)), | |
]; | |
// Calculate months. | |
if ($hasDobOccurred) { | |
$age['months'] = $currentDateTime->month - $dateOfBirth->month; | |
if ($age['months'] > 0 & $currentDateTime->day < $dateOfBirth->day) { | |
--$age['months']; | |
} | |
} else { | |
$age['months'] = self::MAX_MONTHS - 1 - abs($currentDateTime->month - $dateOfBirth->month); | |
} | |
// Calculate days. | |
$currentMonth = $dateOfBirth->month + $age['months']; | |
if ($currentMonth > self::MAX_MONTHS) { | |
$currentMonth = abs($currentMonth - self::MAX_MONTHS); | |
} | |
if ($currentMonth === $currentDateTime->month) { | |
$age['days'] = ($currentDateTime->day - ($dateOfBirth->day - $extraDay)); | |
} else { | |
$age['days'] = ( | |
cal_days_in_month(CAL_GREGORIAN, $currentMonth, $dateOfBirth->year + $age['years']) - | |
($dateOfBirth->day - ($hasDobOccurred ? $extraDay : 0)) + $currentDateTime->day | |
); | |
} | |
return $age; | |
} | |
/** | |
* Demonstrate calculate age simple taking into account leap year. | |
* | |
* @param Carbon $dateOfBirth | |
* @return int | |
*/ | |
public function age(Carbon $dateOfBirth): int | |
{ | |
$today = Carbon::today(); | |
$age = $today->year - $dateOfBirth->year; | |
if ($dateOfBirth->toDate() > $today->addYears(-$age)) { | |
$age--; | |
} | |
return $age; | |
} | |
/** | |
* Demonstrate Carbon age calculation. | |
* | |
* @param string $datOfBirth | |
* @return int | |
*/ | |
public function CarbonAge(string $datOfBirth): int | |
{ | |
return Carbon::parse($datOfBirth)->age; | |
} | |
/** | |
* Demonstrate Carbon date difference in days. | |
* | |
* @param Carbon $dateOfBirth | |
* @return int | |
*/ | |
public function days(Carbon $dateOfBirth): int | |
{ | |
return $dateOfBirth->diffInDays(Carbon::now()); | |
} | |
/** | |
* Demonstrate Carbon date difference in hours. | |
* | |
* @param Carbon $dateOfBirth | |
* @return int | |
*/ | |
public function hours(Carbon $dateOfBirth): int | |
{ | |
return $dateOfBirth->diffInHours(Carbon::now()); | |
} | |
/** | |
* Demonstrate Carbon date difference in seconds. | |
* | |
* @param Carbon $dateOfBirth | |
* @return int | |
*/ | |
public function seconds(Carbon $dateOfBirth): int | |
{ | |
return $dateOfBirth->diffInSeconds(Carbon::now()); | |
} | |
/** | |
* Demonstrate Carbon's parsing a date diff in a string format. | |
* | |
* @param Carbon $dateOfBirth | |
* @return string | |
*/ | |
public function toHumanReadable(Carbon $dateOfBirth): string | |
{ | |
return $dateOfBirth->diff(Carbon::now())->format('I am %y years, %m months ,%d days and %h hours old.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment