root/trunk/lib/helper/MoreDateHelper.php

Revision 2, 2.7 kB (checked in by nperriault, 8 months ago)

First commit of the extracted code from production, I hope no passwd has been forgotten :-)

Line 
1 <?php
2 /**
3  * True I18n extraction ready <code>distance_of_time_in_words()</code> function
4  *
5  * @param  int      $from_time
6  * @param  int      $to_time
7  * @param  boolean  $include_seconds
8  * @return string
9  * @see    distance_of_time_in_words()
10  */
11 function my_distance_of_time_in_words($from_time, $to_time = null, $include_seconds = false)
12 {
13   $to_time = $to_time? $to_time: time();
14
15   $distance_in_minutes = floor(abs($to_time - $from_time) / 60);
16   $distance_in_seconds = floor(abs($to_time - $from_time));
17
18   $string = '';
19   $parameters = array();
20
21   if ($distance_in_minutes <= 1)
22   {
23     if (!$include_seconds)
24     {
25       $string = $distance_in_minutes == 0 ? 'less than a minute' : '1 minute';
26     }
27     else
28     {
29       if ($distance_in_seconds <= 5)
30       {
31         $string = __('less than 5 seconds');
32       }
33       else if ($distance_in_seconds >= 6 && $distance_in_seconds <= 10)
34       {
35         $string = __('less than 10 seconds');
36       }
37       else if ($distance_in_seconds >= 11 && $distance_in_seconds <= 20)
38       {
39         $string = __('less than 20 seconds');
40       }
41       else if ($distance_in_seconds >= 21 && $distance_in_seconds <= 40)
42       {
43         $string = __('half a minute');
44       }
45       else if ($distance_in_seconds >= 41 && $distance_in_seconds <= 59)
46       {
47         $string = __('less than a minute');
48       }
49       else
50       {
51         $string = __('1 minute');
52       }
53     }
54   }
55   else if ($distance_in_minutes >= 2 && $distance_in_minutes <= 44)
56   {
57     $string = __('%minutes% minutes');
58     $parameters['%minutes%'] = $distance_in_minutes;
59   }
60   else if ($distance_in_minutes >= 45 && $distance_in_minutes <= 89)
61   {
62     $string = __('about 1 hour');
63   }
64   else if ($distance_in_minutes >= 90 && $distance_in_minutes <= 1439)
65   {
66     $string = __('about %hours% hours');
67     $parameters['%hours%'] = round($distance_in_minutes / 60);
68   }
69   else if ($distance_in_minutes >= 1440 && $distance_in_minutes <= 2879)
70   {
71     $string = __('1 day');
72   }
73   else if ($distance_in_minutes >= 2880 && $distance_in_minutes <= 43199)
74   {
75     $string = __('%days% days');
76     $parameters['%days%'] = round($distance_in_minutes / 1440);
77   }
78   else if ($distance_in_minutes >= 43200 && $distance_in_minutes <= 86399)
79   {
80     $string = __('about 1 month');
81   }
82   else if ($distance_in_minutes >= 86400 && $distance_in_minutes <= 525959)
83   {
84     $string = __('%months% months');
85     $parameters['%months%'] = round($distance_in_minutes / 43200);
86   }
87   else if ($distance_in_minutes >= 525960 && $distance_in_minutes <= 1051919)
88   {
89     $string = __('about 1 year');
90   }
91   else
92   {
93     $string = __('over %years% years');
94     $parameters['%years%'] = round($distance_in_minutes / 525960);
95   }
96
97   return strtr($string, $parameters);
98 }
99
Note: See TracBrowser for help on using the browser.