]> git.mxchange.org Git - friendica.git/commitdiff
Fix birthday display and setting
authorHypolite Petovan <hypolite@mrpetovan.com>
Thu, 16 Jan 2020 03:35:30 +0000 (22:35 -0500)
committerHypolite Petovan <hypolite@mrpetovan.com>
Mon, 20 Jan 2020 12:47:10 +0000 (07:47 -0500)
- Add new translation string

mod/profiles.php
src/Model/Profile.php
src/Util/Temporal.php

index 551ae54f9b46fa5a459687489383fc71e0b1867d..53da8510c40472904a1e6920aabc7d0250c434ee 100644 (file)
@@ -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')],
index 4546229f7cee562dad65c827d78db851ea76f532..b993562cecc76ab140266ae3e48458f49a114336 100644 (file)
@@ -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']) {
index ecbfcae5432d398519dfe7595ca0d302414900ec..ef27b54b2d00547e292a629a7164861b69ad3842 100644 (file)
@@ -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');
        }
 
        /**