瀏覽代碼

Use TimeAgo class to handle "5 minutes ago"

Julio 9 年之前
父節點
當前提交
8e456de62e

+ 2 - 1
composer.json

@@ -142,7 +142,8 @@
         "a2lix/translation-form-bundle": "~2.0",
         "knplabs/doctrine-behaviors": "~1.1",
         "lunetics/locale-bundle": "^2.4",
-        "theodo-evolution/session-bundle": "1.0.*"
+        "theodo-evolution/session-bundle": "1.0.*",
+        "jimmiw/php-time-ago": "^0.4.11"
     },
     "require-dev": {
         "behat/behat": "@stable",

+ 1 - 1
main/document/document.php

@@ -1784,7 +1784,7 @@ if (isset($documentAndFolders) && is_array($documentAndFolders)) {
 
             // Last edit date
             $last_edit_date = api_get_local_time($document_data['lastedit_date']);
-            $display_date = date_to_str_ago($last_edit_date).
+            $display_date = date_to_str_ago($document_data['lastedit_date']).
                 ' <div class="muted"><small>'.$last_edit_date."</small></div>";
 
             $row[] = $invisibility_span_open.$display_date.$invisibility_span_close;

+ 2 - 2
main/document/edit_document.php

@@ -475,8 +475,8 @@ if ($owner_id == api_get_user_id() ||
 
 	if (!$group_document && !DocumentManager::is_my_shared_folder(api_get_user_id(), $currentDirPath, $sessionId)) {
 		// Updated on field
-		$last_edit_date = api_get_local_time($last_edit_date);
-        $display_date = date_to_str_ago($last_edit_date).' <span class="dropbox_date">'.api_format_date($last_edit_date).'</span>';
+        $display_date = date_to_str_ago($last_edit_date).
+			' <span class="dropbox_date">'.api_format_date(api_get_local_time($last_edit_date)).'</span>';
 		$form->addElement('static', null, get_lang('UpdatedOn'), $display_date);
 	}
 

+ 3 - 2
main/dropbox/index.php

@@ -382,7 +382,7 @@ if ($action != 'add') {
 				$dropbox_file_data[] = $dropbox_file->author;
 
 				$last_upload_date = api_get_local_time($dropbox_file->last_upload_date);
-				$dropbox_file_data[] = date_to_str_ago($last_upload_date).'<br /><span class="dropbox_date">'.
+				$dropbox_file_data[] = date_to_str_ago($dropbox_file->last_upload_date).'<br /><span class="dropbox_date">'.
                     api_format_date($last_upload_date).'</span>';
 
 				$action_icons = check_number_feedback($dropbox_file->id, $number_feedback).' '.get_lang('Feedback').'
@@ -554,7 +554,8 @@ if ($action != 'add') {
 				$receivers_celldata = trim(trim($receivers_celldata), ','); // Removing the trailing comma.
 				$dropbox_file_data[] = $receivers_celldata;
 				$last_upload_date = api_get_local_time($dropbox_file->last_upload_date);
-				$dropbox_file_data[] = date_to_str_ago($last_upload_date).'<br /><span class="dropbox_date">'.api_format_date($last_upload_date).'</span>';
+				$dropbox_file_data[] = date_to_str_ago($dropbox_file->last_upload_date).'<br /><span class="dropbox_date">'.
+					api_format_date($last_upload_date).'</span>';
 
 				//$dropbox_file_data[] = $dropbox_file->author;
 				$receivers_celldata = '';

+ 3 - 117
main/inc/lib/internationalization.lib.php

@@ -658,125 +658,11 @@ function api_format_date($time, $format = null, $language = null)
  * @author Julio Montoya
  */
 
-function date_to_str_ago($date)
+function date_to_str_ago($date, $timeZone = 'UTC')
 {
-    static $initialized = false;
-    static $today, $yesterday;
-    static $min_decade, $min_year, $min_month, $min_week, $min_day, $min_hour, $min_minute;
-    static $min_decades, $min_years, $min_months, $min_weeks, $min_days, $min_hours, $min_minutes;
-    static $sec_time_time, $sec_time_sing, $sec_time_plu;
+    $timeAgo = new TimeAgo($timeZone, api_get_language_isocode());
 
-    $system_timezone = date_default_timezone_get();
-    date_default_timezone_set(_api_get_timezone());
-
-    if (!$initialized) {
-        $today = get_lang('Today');
-        $yesterday = get_lang('Yesterday');
-
-        $min_decade = get_lang('MinDecade');
-        $min_year = get_lang('MinYear');
-        $min_month = get_lang('MinMonth');
-        $min_week = get_lang('MinWeek');
-        $min_day = get_lang('MinDay');
-        $min_hour = get_lang('MinHour');
-        $min_minute = get_lang('MinMinute');
-
-        $min_decades = get_lang('MinDecades');
-        $min_years = get_lang('MinYears');
-        $min_months = get_lang('MinMonths');
-        $min_weeks = get_lang('MinWeeks');
-        $min_days = get_lang('MinDays');
-        $min_hours = get_lang('MinHours');
-        $min_minutes = get_lang('MinMinutes');
-
-        // original 1
-        //$sec_time=array('century'=>3.1556926*pow(10,9),'decade'=>315569260,'year'=>31556926,'month'=>2629743.83,'week'=>604800,'day'=>86400,'hour'=>3600,'minute'=>60,'second'=>1);
-        //$sec_time=array(get_lang('MinDecade')=>315569260,get_lang('MinYear')=>31556926,get_lang('MinMonth')=>2629743.83,get_lang('MinWeek')=>604800,get_lang('MinDay')=>86400,get_lang('MinHour')=>3600,get_lang('MinMinute')=>60);
-        $sec_time_time = array(315569260, 31556926, 2629743.83, 604800, 86400, 3600, 60);
-        $sec_time_sing = array($min_decade, $min_year, $min_month, $min_week, $min_day, $min_hour, $min_minute);
-        $sec_time_plu = array($min_decades, $min_years, $min_months, $min_weeks, $min_days, $min_hours, $min_minutes);
-        $initialized = true;
-    }
-
-    $dst_date = is_string($date) ? strtotime($date) : $date;
-    // For avoiding calling date() several times
-    $date_array = date('s/i/G/j/n/Y', $dst_date);
-    $date_split = explode('/', $date_array);
-
-    $dst_s = $date_split[0];
-    $dst_m = $date_split[1];
-    $dst_h = $date_split[2];
-    $dst_day = $date_split[3];
-    $dst_mth = $date_split[4];
-    $dst_yr = $date_split[5];
-
-    $dst_date = mktime($dst_h, $dst_m, $dst_s, $dst_mth, $dst_day, $dst_yr);
-    $time = $offset = time() - $dst_date; // Seconds between current days and today.
-
-    // Here start the functions sec_to_str()
-    $act_day = date('d');
-    $act_mth = date('n');
-    $act_yr = date('Y');
-
-    if ($dst_day == $act_day && $dst_mth == $act_mth && $dst_yr == $act_yr) {
-        return $today;
-    }
-
-    if ($dst_day == $act_day - 1 && $dst_mth == $act_mth && $dst_yr == $act_yr) {
-        return $yesterday;
-    }
-
-    $str_result = array();
-    $time_result = array();
-    $key_result = array();
-
-    $str = '';
-    $i = 0;
-    for ($i = 0; $i < count($sec_time_time); $i++) {
-        $seconds = $sec_time_time[$i];
-        if ($seconds > $time) {
-            continue;
-        }
-        $current_value = intval($time/$seconds);
-
-        if ($current_value != 1) {
-            $date_str = $sec_time_plu[$i];
-        } else {
-            $date_str = $sec_time_sing[$i];
-
-        }
-        $key_result[] = $sec_time_sing[$i];
-
-        $str_result[] = $current_value.' '.$date_str;
-        $time_result[] = $current_value;
-        $str .= $current_value.$date_str;
-        $time %= $seconds;
-    }
-
-    if (!empty($key_result)) {
-        if ($key_result[0] == $min_day && $key_result[1]== $min_minute) {
-            $key_result[1] = ' 0 '.$min_hours;
-            $str_result[0] = $time_result[0].' '.$key_result[0];
-            $str_result[1] = $key_result[1];
-        }
-
-        if ($key_result[0] == $min_year && ($key_result[1] == $min_day || $key_result[1] == $min_week)) {
-            $key_result[1] = ' 0 '.$min_months;
-            $str_result[0] = $time_result[0].' '.$key_result[0];
-            $str_result[1] = $key_result[1];
-        }
-    }
-
-    if (!empty($str_result)) {
-        if (!empty($str_result[1])) {
-            $str = $str_result[0].', '.$str_result[1];
-        } else {
-            $str = $str_result[0];
-        }
-    }
-
-    date_default_timezone_set($system_timezone);
-    return $str;
+    return $timeAgo->inWords($date);
 }
 
 /**

+ 2 - 1
main/inc/lib/message.lib.php

@@ -1193,7 +1193,8 @@ class MessageManager
                     if (!empty($topic['update_date']) &&
                         $topic['update_date'] != '0000-00-00 00:00:00'
                     ) {
-                        $date .= '<div class="message-group-date" > <i>'.get_lang('LastUpdate').' '.date_to_str_ago($topic['update_date']).'</i></div>';
+                        $date .= '<div class="message-group-date" >
+                            <i>'.get_lang('LastUpdate').' '.date_to_str_ago($topic['update_date']).'</i></div>';
                     }
                 } else {
                     $date .= '<div class="message-group-date"> <i>'.get_lang('Created').' '.date_to_str_ago($topic['send_date']).'</i></div>';

+ 2 - 2
main/inc/lib/notebook.lib.php

@@ -274,7 +274,7 @@ class NotebookManager
 
             $updateValue = '';
             if ($row['update_date'] <> $row['creation_date']) {
-                $updateValue = ', ' . get_lang('UpdateDate') . ': ' . date_to_str_ago($update_date) . '&nbsp;&nbsp;<span class="dropbox_date">' . $update_date . '</span>';
+                $updateValue = ', ' . get_lang('UpdateDate') . ': ' . date_to_str_ago($row['update_date']) . '&nbsp;&nbsp;<span class="dropbox_date">' . $update_date . '</span>';
             }
 
             $actions = '<a href="' . api_get_self() . '?action=editnote&notebook_id=' . $row['notebook_id'] . '">' .
@@ -285,7 +285,7 @@ class NotebookManager
             echo Display::panel(
                 $row['description'],
                 $row['title'] . $session_img.' <div class="pull-right">'.$actions.'</div>',
-                get_lang('CreationDate') . ': ' . date_to_str_ago($creation_date) . '&nbsp;&nbsp;<span class="dropbox_date">' . $creation_date . $updateValue."</span>"
+                get_lang('CreationDate') . ': ' . date_to_str_ago($row['creation_date']) . '&nbsp;&nbsp;<span class="dropbox_date">' . $creation_date . $updateValue."</span>"
             );
         }
     }

+ 1 - 1
main/work/work.lib.php

@@ -2059,7 +2059,7 @@ function get_work_user_list(
                 $work_date = api_convert_and_format_date($work['sent_date']);
 
                 $work['sent_date_from_db'] = $work['sent_date'];
-                $work['sent_date'] = '<div class="date-time">' . date_to_str_ago(api_get_local_time($work['sent_date'])) . ' ' . $add_string . ' ' . $work_date . '</div>';
+                $work['sent_date'] = '<div class="date-time">' . date_to_str_ago($work['sent_date']). ' ' . $add_string . ' ' . $work_date . '</div>';
 
                 // Actions.
                 $correction = '';