Fix birthday display and setting

- Add new translation string
This commit is contained in:
Hypolite Petovan 2020-01-15 22:35:30 -05:00
parent 1c50bc58e4
commit db518e7ef2
3 changed files with 18 additions and 32 deletions

View file

@ -598,7 +598,7 @@ function profiles_content(App $a) {
'$region' => ['region', DI::l10n()->t('Region/State:'), $r[0]['region']], '$region' => ['region', DI::l10n()->t('Region/State:'), $r[0]['region']],
'$postal_code' => ['postal_code', DI::l10n()->t('Postal/Zip Code:'), $r[0]['postal-code']], '$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']], '$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'])), '$gender' => DI::l10n()->t(ContactSelector::gender($r[0]['gender'])),
'$marital' => ['selector' => ContactSelector::maritalStatus($r[0]['marital']), 'value' => DI::l10n()->t($r[0]['marital'])], '$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')], '$with' => ['with', DI::l10n()->t("Who: \x28if applicable\x29"), strip_tags($r[0]['with']), DI::l10n()->t('Examples: cathy123, Cathy Williams, cathy@example.com')],

View file

@ -774,9 +774,9 @@ class Profile
if (!empty($a->profile['dob']) if (!empty($a->profile['dob'])
&& $a->profile['dob'] > DBA::NULL_DATE && $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']) { if ($a->profile['marital']) {

View file

@ -131,12 +131,15 @@ class Temporal
if ($dob < '0000-01-01') { if ($dob < '0000-01-01') {
$value = ''; $value = '';
$age = 0;
} elseif ($dob < '0001-00-00') {
$value = substr($dob, 5);
$age = 0;
} else { } 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"); $tpl = Renderer::getMarkupTemplate("field_input.tpl");
$o = Renderer::replaceMacros($tpl, $o = Renderer::replaceMacros($tpl,
[ [
@ -144,7 +147,7 @@ class Temporal
'dob', 'dob',
DI::l10n()->t('Birthday:'), DI::l10n()->t('Birthday:'),
$value, $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') . '"' 'placeholder="' . DI::l10n()->t('YYYY-MM-DD or MM-DD') . '"'
] ]
@ -339,48 +342,31 @@ class Temporal
/** /**
* Returns timezone correct age in years. * Returns timezone correct age in years.
* *
* Returns the age in years, given a date of birth, the timezone of the person * Returns the age in years, given a date of birth and the timezone of the person
* whose date of birth is provided, and the timezone of the person viewing the * whose date of birth is provided.
* 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.
* *
* @param string $dob Date of Birth * @param string $dob Date of Birth
* @param string $owner_tz (optional) Timezone of the person of interest * @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 * @return int Age in years
* @throws \Exception * @throws \Exception
*/ */
public static function getAgeByTimezone($dob, $owner_tz = '', $viewer_tz = '') public static function getAgeByTimezone($dob, $owner_tz = '')
{ {
if (!intval($dob)) { if (!intval($dob)) {
return 0; return 0;
} }
if (!$owner_tz) { if (!$owner_tz) {
$owner_tz = date_default_timezone_get(); $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'); $birthdate = new DateTime($dob . ' 00:00:00', new DateTimeZone($owner_tz));
list($year, $month, $day) = explode("-", $birthdate); $currentDate = new DateTime('now', new DateTimeZone('UTC'));
$year_diff = DateTimeFormat::timezoneNow($viewer_tz, 'Y') - $year;
$curr_month = DateTimeFormat::timezoneNow($viewer_tz, 'm');
$curr_day = DateTimeFormat::timezoneNow($viewer_tz, 'd');
if (($curr_month < $month) || (($curr_month == $month) && ($curr_day < $day))) { $interval = $birthdate->diff($currentDate);
$year_diff--;
}
return $year_diff; return $interval->format('%y');
} }
/** /**