*
* @param date $birthDate
* Birth Date.
+ * @param date $targetDate
+ * Target Date. (show age on specific date)
*
* @return int
* array $results contains years or months
*/
- public static function calculateAge($birthDate) {
+ public static function calculateAge($birthDate, $targetDate = NULL) {
$results = [];
$formatedBirthDate = CRM_Utils_Date::customFormat($birthDate, '%Y-%m-%d');
$birthYear = $bDate[0];
$birthMonth = $bDate[1];
$birthDay = $bDate[2];
- $year_diff = date("Y") - $birthYear;
+ $targetDate = strtotime($targetDate ?? date('Y-m-d'));
+
+ $year_diff = date("Y", $targetDate) - $birthYear;
// don't calculate age CRM-3143
if ($birthYear == '1902') {
return $results;
}
-
switch ($year_diff) {
case 1:
- $month = (12 - $birthMonth) + date("m");
+ $month = (12 - $birthMonth) + date("m", $targetDate);
if ($month < 12) {
- if (date("d") < $birthDay) {
+ if (date("d", $targetDate) < $birthDay) {
$month--;
}
$results['months'] = $month;
}
- elseif ($month == 12 && (date("d") < $birthDay)) {
+ elseif ($month == 12 && (date("d", $targetDate) < $birthDay)) {
$results['months'] = $month - 1;
}
else {
break;
case 0:
- $month = date("m") - $birthMonth;
+ $month = date("m", $targetDate) - $birthMonth;
$results['months'] = $month;
break;
default:
$results['years'] = $year_diff;
- if ((date("m") < $birthMonth) || (date("m") == $birthMonth) && (date("d") < $birthDay)) {
+ if ((date("m", $targetDate) < $birthMonth) || (date("m", $targetDate) == $birthMonth) && (date("d", $targetDate) < $birthDay)) {
$results['years']--;
}
}
*/
public static function intervalAdd($unit, $interval, $date, $dontCareTime = FALSE) {
if (is_array($date)) {
- $hour = $date['H'] ?? NULL;
- $minute = $date['i'] ?? NULL;
- $second = $date['s'] ?? NULL;
+ $hour = $date['H'] ?? '00';
+ $minute = $date['i'] ?? '00';
+ $second = $date['s'] ?? '00';
$month = $date['M'] ?? NULL;
$day = $date['d'] ?? NULL;
$year = $date['Y'] ?? NULL;