From db518e7ef2fef6d4b7eaed63bc5320620d7cb471 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Wed, 15 Jan 2020 22:35:30 -0500 Subject: [PATCH] Fix birthday display and setting - Add new translation string --- mod/profiles.php | 2 +- src/Model/Profile.php | 4 ++-- src/Util/Temporal.php | 44 +++++++++++++++---------------------------- 3 files changed, 18 insertions(+), 32 deletions(-) diff --git a/mod/profiles.php b/mod/profiles.php index 551ae54f9b..53da8510c4 100644 --- a/mod/profiles.php +++ b/mod/profiles.php @@ -598,7 +598,7 @@ function profiles_content(App $a) { '$region' => ['region', DI::l10n()->t('Region/State:'), $r[0]['region']], '$postal_code' => ['postal_code', DI::l10n()->t('Postal/Zip Code:'), $r[0]['postal-code']], '$country_name' => ['country_name', DI::l10n()->t('Country:'), $r[0]['country-name']], - '$age' => ((intval($r[0]['dob'])) ? '(' . DI::l10n()->t('Age: ') . Temporal::getAgeByTimezone($r[0]['dob'],$a->user['timezone'],$a->user['timezone']) . ')' : ''), + '$age' => ((intval($r[0]['dob'])) ? '(' . DI::l10n()->t('Age: ') . DI::l10n()->tt('%d year old', '%d years old', Temporal::getAgeByTimezone($r[0]['dob'], $a->user['timezone'])) . ')' : ''), '$gender' => DI::l10n()->t(ContactSelector::gender($r[0]['gender'])), '$marital' => ['selector' => ContactSelector::maritalStatus($r[0]['marital']), 'value' => DI::l10n()->t($r[0]['marital'])], '$with' => ['with', DI::l10n()->t("Who: \x28if applicable\x29"), strip_tags($r[0]['with']), DI::l10n()->t('Examples: cathy123, Cathy Williams, cathy@example.com')], diff --git a/src/Model/Profile.php b/src/Model/Profile.php index 4546229f7c..b993562cec 100644 --- a/src/Model/Profile.php +++ b/src/Model/Profile.php @@ -774,9 +774,9 @@ class Profile if (!empty($a->profile['dob']) && $a->profile['dob'] > DBA::NULL_DATE - && $age = Temporal::getAgeByTimezone($a->profile['dob'], $a->profile['timezone'], '') + && $age = Temporal::getAgeByTimezone($a->profile['dob'], $a->profile['timezone']) ) { - $profile['age'] = [DI::l10n()->t('Age:'), $age]; + $profile['age'] = [DI::l10n()->t('Age: ') , DI::l10n()->tt('%d year old', '%d years old', $age)]; } if ($a->profile['marital']) { diff --git a/src/Util/Temporal.php b/src/Util/Temporal.php index ecbfcae543..ef27b54b2d 100644 --- a/src/Util/Temporal.php +++ b/src/Util/Temporal.php @@ -131,12 +131,15 @@ class Temporal if ($dob < '0000-01-01') { $value = ''; + $age = 0; + } elseif ($dob < '0001-00-00') { + $value = substr($dob, 5); + $age = 0; } else { - $value = DateTimeFormat::utc(($year > 1000) ? $dob : '1000-' . $month . '-' . $day, 'Y-m-d'); + $value = DateTimeFormat::utc($dob, 'Y-m-d'); + $age = self::getAgeByTimezone($value, $timezone); } - $age = (intval($value) ? self::getAgeByTimezone($value, $timezone, $timezone) : ""); - $tpl = Renderer::getMarkupTemplate("field_input.tpl"); $o = Renderer::replaceMacros($tpl, [ @@ -144,7 +147,7 @@ class Temporal 'dob', DI::l10n()->t('Birthday:'), $value, - intval($age) > 0 ? DI::l10n()->t('Age: ') . $age : "", + intval($age) > 0 ? DI::l10n()->t('Age: ') . DI::l10n()->tt('%d year old', '%d years old', $age) : '', '', 'placeholder="' . DI::l10n()->t('YYYY-MM-DD or MM-DD') . '"' ] @@ -339,48 +342,31 @@ class Temporal /** * Returns timezone correct age in years. * - * Returns the age in years, given a date of birth, the timezone of the person - * whose date of birth is provided, and the timezone of the person viewing the - * result. - * - * Why? Bear with me. Let's say I live in Mittagong, Australia, and my birthday - * is on New Year's. You live in San Bruno, California. - * When exactly are you going to see my age increase? - * - * A: 5:00 AM Dec 31 San Bruno time. That's precisely when I start celebrating - * and become a year older. If you wish me happy birthday on January 1 - * (San Bruno time), you'll be a day late. + * Returns the age in years, given a date of birth and the timezone of the person + * whose date of birth is provided. * * @param string $dob Date of Birth * @param string $owner_tz (optional) Timezone of the person of interest - * @param string $viewer_tz (optional) Timezone of the person viewing * * @return int Age in years * @throws \Exception */ - public static function getAgeByTimezone($dob, $owner_tz = '', $viewer_tz = '') + public static function getAgeByTimezone($dob, $owner_tz = '') { if (!intval($dob)) { return 0; } + if (!$owner_tz) { $owner_tz = date_default_timezone_get(); } - if (!$viewer_tz) { - $viewer_tz = date_default_timezone_get(); - } - $birthdate = DateTimeFormat::convert($dob . ' 00:00:00+00:00', $owner_tz, 'UTC', 'Y-m-d'); - list($year, $month, $day) = explode("-", $birthdate); - $year_diff = DateTimeFormat::timezoneNow($viewer_tz, 'Y') - $year; - $curr_month = DateTimeFormat::timezoneNow($viewer_tz, 'm'); - $curr_day = DateTimeFormat::timezoneNow($viewer_tz, 'd'); + $birthdate = new DateTime($dob . ' 00:00:00', new DateTimeZone($owner_tz)); + $currentDate = new DateTime('now', new DateTimeZone('UTC')); - if (($curr_month < $month) || (($curr_month == $month) && ($curr_day < $day))) { - $year_diff--; - } + $interval = $birthdate->diff($currentDate); - return $year_diff; + return $interval->format('%y'); } /**