Development

/branches/1.0/lib/helper/DateHelper.php

You must first sign up to be able to contribute.

root/branches/1.0/lib/helper/DateHelper.php

Revision 8763, 4.3 kB (checked in by fabien, 6 years ago)

fixed DateHelper? distance_of_time_in_words() generates incorrect output for some dates (closes #3322)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 /**
12  * DateHelper.
13  *
14  * @package    symfony
15  * @subpackage helper
16  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
17  * @version    SVN: $Id$
18  */
19
20 function format_daterange($start_date, $end_date, $format = 'd', $full_text, $start_text, $end_text, $culture = null, $charset = null)
21 {
22   if ($start_date != '' && $end_date != '')
23   {
24     return sprintf($full_text, format_date($start_date, $format, $culture, $charset), format_date($end_date, $format, $culture, $charset));
25   }
26   else if ($start_date != '')
27   {
28     return sprintf($start_text, format_date($start_date, $format, $culture, $charset));
29   }
30   else if ($end_date != '')
31   {
32     return sprintf($end_text, format_date($end_date, $format, $culture, $charset));
33   }
34 }
35
36 function format_date($date, $format = 'd', $culture = null, $charset = null)
37 {
38   static $dateFormats = array();
39
40   if (is_null($date))
41   {
42     return null;
43   }
44
45   if (!$culture)
46   {
47     $culture = sfContext::getInstance()->getUser()->getCulture();
48   }
49
50   if (!$charset)
51   {
52     $charset = sfConfig::get('sf_charset');
53   }
54
55   if (!isset($dateFormats[$culture]))
56   {
57     $dateFormats[$culture] = new sfDateFormat($culture);
58   }
59
60   return $dateFormats[$culture]->format($date, $format, null, $charset);
61 }
62
63 function format_datetime($date, $format = 'F', $culture = null, $charset = null)
64 {
65   return format_date($date, $format, $culture, $charset);
66 }
67
68 function distance_of_time_in_words($from_time, $to_time = null, $include_seconds = false)
69 {
70   $to_time = $to_time? $to_time: time();
71
72   $distance_in_minutes = floor(abs($to_time - $from_time) / 60);
73   $distance_in_seconds = floor(abs($to_time - $from_time));
74
75   $string = '';
76   $parameters = array();
77
78   if ($distance_in_minutes <= 1)
79   {
80     if (!$include_seconds)
81     {
82       $string = $distance_in_minutes == 0 ? 'less than a minute' : '1 minute';
83     }
84     else
85     {
86       if ($distance_in_seconds <= 5)
87       {
88         $string = 'less than 5 seconds';
89       }
90       else if ($distance_in_seconds >= 6 && $distance_in_seconds <= 10)
91       {
92         $string = 'less than 10 seconds';
93       }
94       else if ($distance_in_seconds >= 11 && $distance_in_seconds <= 20)
95       {
96         $string = 'less than 20 seconds';
97       }
98       else if ($distance_in_seconds >= 21 && $distance_in_seconds <= 40)
99       {
100         $string = 'half a minute';
101       }
102       else if ($distance_in_seconds >= 41 && $distance_in_seconds <= 59)
103       {
104         $string = 'less than a minute';
105       }
106       else
107       {
108         $string = '1 minute';
109       }
110     }
111   }
112   else if ($distance_in_minutes >= 2 && $distance_in_minutes <= 44)
113   {
114     $string = '%minutes% minutes';
115     $parameters['%minutes%'] = $distance_in_minutes;
116   }
117   else if ($distance_in_minutes >= 45 && $distance_in_minutes <= 89)
118   {
119     $string = 'about 1 hour';
120   }
121   else if ($distance_in_minutes >= 90 && $distance_in_minutes <= 1439)
122   {
123     $string = 'about %hours% hours';
124     $parameters['%hours%'] = round($distance_in_minutes / 60);
125   }
126   else if ($distance_in_minutes >= 1440 && $distance_in_minutes <= 2879)
127   {
128     $string = '1 day';
129   }
130   else if ($distance_in_minutes >= 2880 && $distance_in_minutes <= 43199)
131   {
132     $string = '%days% days';
133     $parameters['%days%'] = round($distance_in_minutes / 1440);
134   }
135   else if ($distance_in_minutes >= 43200 && $distance_in_minutes <= 86399)
136   {
137     $string = 'about 1 month';
138   }
139   else if ($distance_in_minutes >= 86400 && $distance_in_minutes <= 525959)
140   {
141     $string = '%months% months';
142     $parameters['%months%'] = round($distance_in_minutes / 43200);
143   }
144   else if ($distance_in_minutes >= 525960 && $distance_in_minutes <= 1051919)
145   {
146     $string = 'about 1 year';
147   }
148   else
149   {
150     $string = 'over %years% years';
151     $parameters['%years%'] = floor($distance_in_minutes / 525960);
152   }
153
154   if (sfConfig::get('sf_i18n'))
155   {
156     use_helper('I18N');
157
158     return __($string, $parameters);
159   }
160   else
161   {
162     return strtr($string, $parameters);
163   }
164 }
165
166 // Like distance_of_time_in_words, but where to_time is fixed to time()
167 function time_ago_in_words($from_time, $include_seconds = false)
168 {
169   return distance_of_time_in_words($from_time, time(), $include_seconds);
170 }
171
Note: See TracBrowser for help on using the browser.