Development

/branches/1.0/lib/helper/DateFormHelper.php

You must first sign up to be able to contribute.

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

Revision 14120, 38.7 kB (checked in by FabianLange, 5 years ago)

[1.0] fixed select_timezone_tag not possible to display city. fixes #5359

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 use_helper('Form');
4
5 /*
6  * This file is part of the symfony package.
7  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
8  *
9  * For the full copyright and license information, please view the LICENSE
10  * file that was distributed with this source code.
11  */
12
13 /**
14  * DateFormHelper.
15  *
16  * @package    symfony
17  * @subpackage helper
18  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
19  * @version    SVN: $Id$
20  */
21
22 /**
23  * Returns a <select> tag populated with all the days of the month (1 - 31).
24  *
25  * By default, the <i>$value</i> parameter is set to today's day. To override this, simply pass an integer
26  * (1 - 31) to the <i>$value</i> parameter. You can also set the <i>$value</i> parameter to null which will disable
27  * the <i>$value</i>, however this will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i>
28  * parameter. For convenience, symfony also offers the select_date_tag helper function which combines the
29  * select_year_tag, select_month_tag, and select_day_tag functions into a single helper.
30  *
31  * <b>Options:</b>
32  * - include_blank  - Includes a blank <option> tag at the beginning of the string with an empty value.
33  * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
34  *
35  * <b>Examples:</b>
36  * <code>
37  *  echo select_day_tag('day', 14);
38  * </code>
39  *
40  * @param  string field name
41  * @param  integer selected value (1 - 31)
42  * @param  array  additional HTML compliant <select> tag parameters
43  * @return string <select> tag populated with all the days of the month (1 - 31).
44  * @see    select_date_tag, select datetime_tag
45  */
46 function select_day_tag($name, $value = null, $options = array(), $html_options = array())
47 {
48   if ($value === null)
49   {
50     $value = date('j');
51   }
52
53   $options = _parse_attributes($options);
54
55   $select_options = array();
56   _convert_include_custom_for_select($options, $select_options);
57
58   for ($x = 1; $x < 32; $x++)
59   {
60     $select_options[$x] = str_pad($x, 2, '0', STR_PAD_LEFT);
61   }
62
63   return select_tag($name, options_for_select($select_options, $value), $html_options);
64 }
65
66 /**
67  * Returns a <select> tag populated with all the months of the year (1 - 12).
68  *
69  * By default, the <i>$value</i> parameter is set to today's month. To override this, simply pass an integer
70  * (1 - 12) to the <i>$value</i> parameter. You can also set the <i>$value</i> parameter to null which will disable
71  * the <i>$value</i>, however this will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i>
72  * parameter. Also, the each month's display title is set to return its respective full month name, which can be easily
73  * overridden by passing the 'use_short_names' or 'use_month_numbers' options to the <i>$options</i> parameter.
74  * For convenience, Symfony also offers the select_date_tag helper function which combines the
75  * select_year_tag, select_month_tag, and select_day_tag functions into a single helper.
76  *
77  * <b>Options:</b>
78  * - include_blank     - Includes a blank <option> tag at the beginning of the string with an empty value
79  * - include_custom    - Includes an <option> tag with a custom display title at the beginning of the string with an empty value
80  * - use_month_numbers - If set to true, will show the month's numerical value (1 - 12) instead of the months full name.
81  * - use_short_month   - If set to true, will show the month's short name (i.e. Jan, Feb, Mar) instead of its full name.
82  * 
83  * <b>Examples:</b>
84  * <code>
85  *  echo select_month_tag('month', 5, array('use_short_month' => true));
86  * </code>
87  *
88  * <code>
89  *  echo select_month_tag('month', null, array('use_month_numbers' => true, 'include_blank' => true));
90  * </code>
91  *
92  * @param  string field name
93  * @param  integer selected value (1 - 12)
94  * @param  array  additional HTML compliant <select> tag parameters
95  * @return string <select> tag populated with all the months of the year (1 - 12).
96  * @see select_date_tag, select datetime_tag
97  */
98 function select_month_tag($name, $value = null, $options = array(), $html_options = array())
99 {
100   if ($value === null)
101   {
102     $value = date('n');
103   }
104
105   $options = _parse_attributes($options);
106
107   $select_options = array();
108   _convert_include_custom_for_select($options, $select_options);
109
110   if (_get_option($options, 'use_month_numbers'))
111   {
112     for ($k = 1; $k < 13; $k++)
113     {
114       $select_options[$k] = str_pad($k, 2, '0', STR_PAD_LEFT);
115     }
116   }
117   else
118   {
119     $culture = _get_option($options, 'culture', sfContext::getInstance()->getUser()->getCulture());
120     $I18n_arr = _get_I18n_date_locales($culture);
121
122     if (_get_option($options, 'use_short_month'))
123     {
124       $month_names = $I18n_arr['dateFormatInfo']->getAbbreviatedMonthNames();
125     }
126     else
127     {
128       $month_names = $I18n_arr['dateFormatInfo']->getMonthNames();
129     }
130
131     $add_month_numbers = _get_option($options, 'add_month_numbers');
132     foreach ($month_names as $k => $v)
133     {
134       $select_options[$k + 1] = $add_month_numbers ? ($k + 1).' - '.$v : $v;
135     }
136   }
137
138   return select_tag($name, options_for_select($select_options, $value), $html_options);
139 }
140
141 /**
142  * Returns a <select> tag populated with a range of years.
143  *
144  * By default, the <i>$value</i> parameter is set to today's year. To override this, simply pass a four-digit integer (YYYY)
145  * to the <i>$value</i> parameter. You can also set the <i>$value</i> parameter to null which will disable
146  * the <i>$value</i>, however this will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i>
147  * parameter. Also, the default selectable range of years is set to show five years back and five years forward from today's year.
148  * For instance, if today's year is 2006, the default 'year_start' option will be set to 2001 and the 'year_end' option will be set
149  * to 2011.  These start and end dates can easily be overwritten by setting the 'year_start' and 'year_end' options in the <i>$options</i>
150  * parameter. For convenience, Symfony also offers the select_date_tag helper function which combines the
151  * select_year_tag, select_month_tag, and select_day_tag functions into a single helper.
152  *
153  * <b>Options:</b>
154  * - include_blank  - Includes a blank <option> tag at the beginning of the string with an empty value
155  * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value
156  * - year_start     - If set, the range of years will begin at this four-digit date (i.e. 1979)
157  * - year_end       - If set, the range of years will end at this four-digit date (i.e. 2025)
158  * 
159  * <b>Examples:</b>
160  * <code>
161  *  echo select_year_tag('year');
162  * </code>
163  *
164  * <code>
165  *  $year_start = date('Y', strtotime('-10 years'));
166  *  $year_end = date('Y', strtotime('+10 years'));
167  *  echo select_year_tag('year', null, array('year_start' => $year_start, 'year_end' => $year_end));
168  * </code>
169  *
170  * @param  string field name
171  * @param  integer selected value within the range of years.
172  * @param  array  additional HTML compliant <select> tag parameters
173  * @return string <select> tag populated with a range of years.
174  * @see select_date_tag, select datetime_tag
175  */
176 function select_year_tag($name, $value = null, $options = array(), $html_options = array())
177 {
178   if ($value === null)
179   {
180     $value = date('Y');
181   }
182     
183   $options = _parse_attributes($options);
184
185   $select_options = array();
186   _convert_include_custom_for_select($options, $select_options);
187
188   if (strlen($value) > 0 && is_numeric($value))
189   {
190     $year_origin = $value;
191   }
192   else
193   {
194     $year_origin = date('Y');
195   }
196
197   $year_start = _get_option($options, 'year_start', $year_origin - 5);
198   $year_end   = _get_option($options, 'year_end', $year_origin + 5);
199
200   $ascending  = ($year_start < $year_end);
201   $until_year = ($ascending) ? $year_end + 1 : $year_end - 1;
202
203   for ($x = $year_start; $x != $until_year; ($ascending) ? $x++ : $x--)
204   {
205     $select_options[$x] = $x;
206   }
207
208   return select_tag($name, options_for_select($select_options, $value), $html_options);
209 }
210
211 /**
212  * Returns three <select> tags populated with a range of months, days, and years.
213  *
214  * By default, the <i>$value</i> parameter is set to today's month, day and year. To override this, simply pass a valid date
215  * or a correctly formatted date array (see example) to the <i>$value</i> parameter. You can also set the <i>$value</i>
216  * parameter to null which will disable the <i>$value</i>, however this will only be useful if you pass 'include_blank' or
217  * 'include_custom' to the <i>$options</i> parameter. Also, the default selectable range of years is set to show five years
218  * back and five years forward from today's year. For instance, if today's year is 2006, the default 'year_start' option will
219  * be set to 2001 and the 'year_end' option will be set to 2011.  These start and end dates can easily be overwritten by
220  * setting the 'year_start' and 'year_end' options in the <i>$options</i> parameter.
221  *
222  * <b>Note:</b> The <i>$name</i> parameter will automatically converted to array names. For example, a <i>$name</i> of "date" becomes:
223  * <samp>
224  *  <select name="date[month]">...</select>
225  *  <select name="date[day]">...</select>
226  *  <select name="date[year]">...</select>
227  * </samp>
228  * 
229  * <b>Options:</b>
230  * - include_blank     - Includes a blank <option> tag at the beginning of the string with an empty value.
231  * - include_custom    - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
232  * - discard_month     - If set to true, will only return select tags for day and year.
233  * - discard_day       - If set to true, will only return select tags for month and year.
234  * - discard_year      - If set to true, will only return select tags for month and day.
235  * - use_month_numbers - If set to true, will show the month's numerical value (1 - 12) instead of the months full name.
236  * - use_short_month   - If set to true, will show the month's short name (i.e. Jan, Feb, Mar) instead of its full name.
237  * - year_start        - If set, the range of years will begin at this four-digit date (i.e. 1979)
238  * - year_end          - If set, the range of years will end at this four-digit date (i.e. 2025)
239  * - date_seperator    - Includes a string of defined text between each generated select tag
240  * 
241  * <b>Examples:</b>
242  * <code>
243  *  echo select_date_tag('date');
244  * </code>
245  *
246  * <code>
247  *  echo select_date_tag('date', '2006-10-30');
248  * </code>
249  *
250  * <code>
251  *  $date = array('year' => '1979', 'month' => 10, 'day' => 30);
252  *  echo select_date_tag('date', $date, array('year_start' => $date['year'] - 10, 'year_end' => $date['year'] + 10));
253  * </code>
254  *
255  * @param  string field name (automatically becomes an array of parts: name[year], name[month], year[day])
256  * @param  mixed  accepts a valid date string or properly formatted date array
257  * @param  array  additional HTML compliant <select> tag parameters
258  * @return string three <select> tags populated with a months, days and years
259  * @see select datetime_tag, select_month_tag, select_date_tag, select_year_tag
260  */
261 function select_date_tag($name, $value = null, $options = array(), $html_options = array())
262 {
263   $options = _parse_attributes($options);
264
265   $culture = _get_option($options, 'culture', sfContext::getInstance()->getUser()->getCulture());
266
267   // set it back for month tag
268   $options['culture'] = $culture;
269
270   $I18n_arr = _get_I18n_date_locales($culture);
271
272   $date_seperator = _get_option($options, 'date_seperator', $I18n_arr['date_seperator']);
273   $discard_month  = _get_option($options, 'discard_month');
274   $discard_day    = _get_option($options, 'discard_day');
275   $discard_year   = _get_option($options, 'discard_year');
276
277   // discarding month automatically discards day
278   if ($discard_month)
279   {
280     $discard_day = true;
281   }
282
283   $order = _get_option($options, 'order');
284   $tags = array();
285   if (is_array($order) && count($order) == 3)
286   {
287     foreach ($order as $v)
288     {
289       $tags[] = $v[0];
290     }
291   }
292   else
293   {
294     $tags = $I18n_arr['date_order'];
295   }
296
297   if ($include_custom = _get_option($options, 'include_custom'))
298   {
299     $include_custom_month = is_array($include_custom)
300         ? (isset($include_custom['month']) ? array('include_custom' => $include_custom['month']) : array())
301         : array('include_custom' => $include_custom);
302
303     $include_custom_day = is_array($include_custom)
304         ? (isset($include_custom['day']) ? array('include_custom' => $include_custom['day']) : array())
305         : array('include_custom' => $include_custom);
306
307     $include_custom_year = is_array($include_custom)
308         ? (isset($include_custom['year']) ? array('include_custom' => $include_custom['year']) : array())
309         : array('include_custom' => $include_custom);
310   }
311   else
312   {
313     $include_custom_month = array();
314     $include_custom_day   = array();
315     $include_custom_year  = array();
316   }
317
318   $month_name = $name.'[month]';
319   $m = !$discard_month ? select_month_tag($month_name, _parse_value_for_date($value, 'month', 'm'), $options + $include_custom_month, $html_options) : '';
320
321   $day_name = $name.'[day]';
322   $d = !$discard_day ? select_day_tag($day_name, _parse_value_for_date($value, 'day', 'd'), $options + $include_custom_day, $html_options) : '';
323
324   $year_name = $name.'[year]';
325   $y = !$discard_year ? select_year_tag($year_name, _parse_value_for_date($value, 'year', 'Y'), $options + $include_custom_year, $html_options) : '';
326
327   // we have $tags = array ('m','d','y')
328   foreach ($tags as $k => $v)
329   {
330     // $tags['m|d|y'] = $m|$d|$y
331     if (strlen($$v))
332     {
333       $tags[$k] = $$v;
334     }
335     else
336     {
337       unset($tags[$k]);
338     }
339   }
340
341   return implode($date_seperator, $tags);
342 }
343
344 /**
345  * Returns a <select> tag populated with 60 seconds (0 - 59).
346  *
347  * By default, the <i>$value</i> parameter is set to the current second (right now). To override this, simply pass an integer
348  * (0 - 59) to the <i>$value</i> parameter. You can also set the <i>$value</i> parameter to null which will disable
349  * the <i>$value</i>, however this will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i>
350  * parameter. In many cases, you have no need for all 60 seconds in a minute.  the 'second_step' option in the
351  * <i>$options</i> parameter gives you the ability to define intervals to display.  So for instance you could define 15 as your
352  * 'minute_step' interval and the select tag would return the values 0, 15, 30, and 45. For convenience, Symfony also offers the
353  * select_time_tag select_datetime_tag helper functions which combine other date and time helpers to easily build date and time select boxes.
354  *
355  * <b>Options:</b>
356  * - include_blank  - Includes a blank <option> tag at the beginning of the string with an empty value.
357  * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
358  * - second_step    - If set, the seconds will be incremented in blocks of X, where X = 'second_step'
359  *
360  * <b>Examples:</b>
361  * <code>
362  *  echo select_second_tag('second');
363  * </code>
364  *
365  * <code>
366  *  echo select_second_tag('second', 15, array('second_step' => 15));
367  * </code>
368  *
369  * @param  string field name
370  * @param  integer selected value (0 - 59)
371  * @param  array  additional HTML compliant <select> tag parameters
372  * @return string <select> tag populated with 60 seconds (0 - 59).
373  * @see select_time_tag, select datetime_tag
374  */
375 function select_second_tag($name, $value = null, $options = array(), $html_options = array())
376 {
377   if ($value === null)
378   {
379     $value = date('s');
380   }
381
382   $options = _parse_attributes($options);
383   $select_options = array();
384   _convert_include_custom_for_select($options, $select_options);
385
386   $second_step = _get_option($options, 'second_step', 1);
387   for ($x = 0; $x < 60; $x += $second_step)
388   {
389     $select_options[$x] = str_pad($x, 2, '0', STR_PAD_LEFT);
390   }
391
392   return select_tag($name, options_for_select($select_options, $value), $html_options);
393 }
394
395 /**
396  * Returns a <select> tag populated with 60 minutes (0 - 59).
397  *
398  * By default, the <i>$value</i> parameter is set to the current minute. To override this, simply pass an integer
399  * (0 - 59) to the <i>$value</i> parameter. You can also set the <i>$value</i> parameter to null which will disable
400  * the <i>$value</i>, however this will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i>
401  * parameter. In many cases, you have no need for all 60 minutes in an hour.  the 'minute_step' option in the
402  * <i>$options</i> parameter gives you the ability to define intervals to display.  So for instance you could define 15 as your
403  * 'minute_step' interval and the select tag would return the values 0, 15, 30, and 45. For convenience, Symfony also offers the
404  * select_time_tag select_datetime_tag helper functions which combine other date and time helpers to easily build date and time select boxes.
405  *
406  * <b>Options:</b>
407  * - include_blank  - Includes a blank <option> tag at the beginning of the string with an empty value.
408  * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
409  * - minute_step    - If set, the minutes will be incremented in blocks of X, where X = 'minute_step'
410  *
411  * <b>Examples:</b>
412  * <code>
413  *  echo select_minute_tag('minute');
414  * </code>
415  *
416  * <code>
417  *  echo select_minute_tag('minute', 15, array('minute_step' => 15));
418  * </code>
419  *
420  * @param  string field name
421  * @param  integer selected value (0 - 59)
422  * @param  array  additional HTML compliant <select> tag parameters
423  * @return string <select> tag populated with 60 minutes (0 - 59).
424  * @see select_time_tag, select datetime_tag
425  */
426 function select_minute_tag($name, $value = null, $options = array(), $html_options = array())
427 {
428   if ($value === null)
429   {
430     $value = date('i');
431   }
432
433   $options = _parse_attributes($options);
434   $select_options = array();
435   _convert_include_custom_for_select($options, $select_options);
436
437   $minute_step = _get_option($options, 'minute_step', 1);
438   for ($x = 0; $x < 60; $x += $minute_step)
439   {
440     $select_options[$x] = str_pad($x, 2, '0', STR_PAD_LEFT);
441   }
442
443   return select_tag($name, options_for_select($select_options, $value), $html_options);
444 }
445
446 /**
447  * Returns a <select> tag populated with 24 hours (0 - 23), or optionally 12 hours (1 - 12).
448  *
449  * By default, the <i>$value</i> parameter is set to the current hour. To override this, simply pass an integer
450  * (0 - 23 or 1 - 12 if '12hour_time' = true) to the <i>$value</i> parameter. You can also set the <i>$value</i> parameter to null which will disable
451  * the <i>$value</i>, however this will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i>
452  * parameter. For convenience, Symfony also offers the select_time_tag select_datetime_tag helper functions
453  * which combine other date and time helpers to easily build date and time select boxes.
454  *
455  * <b>Options:</b>
456  * - include_blank  - Includes a blank <option> tag at the beginning of the string with an empty value.
457  * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
458  * - 12hour_time    - If set to true, will return integers 1 through 12 instead of the default 0 through 23 as well as an AM/PM select box.
459  *
460  * <b>Examples:</b>
461  * <code>
462  *  echo select_hour_tag('hour');
463  * </code>
464  *
465  * <code>
466  *  echo select_hour_tag('hour', 6, array('12hour_time' => true));
467  * </code>
468  *
469  * @param  string field name
470  * @param  integer selected value (0 - 23 or 1 - 12 if '12hour_time' = true)
471  * @param  array  additional HTML compliant <select> tag parameters
472  * @return string <select> tag populated with 24 hours (0 - 23), or optionally 12 hours (1 - 12).
473  * @see select_time_tag, select datetime_tag
474  */
475 function select_hour_tag($name, $value = null, $options = array(), $html_options = array())
476 {
477   $options = _parse_attributes($options);
478   $select_options = array();
479   _convert_include_custom_for_select($options, $select_options);
480
481   $_12hour_time = _get_option($options, '12hour_time');
482
483   if ($value === null)
484   {
485     $value = date($_12hour_time ? 'h' : 'H');
486   }
487
488   $start_hour = $_12hour_time ? : 0;
489   $end_hour   = $_12hour_time ? 12 : 23;
490
491   for ($x = $start_hour; $x <= $end_hour; $x++)
492   {
493     $select_options[$x] = str_pad($x, 2, '0', STR_PAD_LEFT);
494   }
495
496   return select_tag($name, options_for_select($select_options, $value), $html_options);
497 }
498
499 /**
500  * Returns a <select> tag populated with AM and PM options for use with 12-Hour time.
501  *
502  * By default, the <i>$value</i> parameter is set to the correct AM/PM setting based on the current time.
503  * To override this, simply pass either AM or PM to the <i>$value</i> parameter. You can also set the
504  * <i>$value</i> parameter to null which will disable the <i>$value</i>, however this will only be
505  * useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i> parameter. For
506  * convenience, Symfony also offers the select_time_tag select_datetime_tag helper functions
507  * which combine other date and time helpers to easily build date and time select boxes.
508  *
509  * <b>Options:</b>
510  * - include_blank  - Includes a blank <option> tag at the beginning of the string with an empty value.
511  * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
512  *
513  * <b>Examples:</b>
514  * <code>
515  *  echo select_ampm_tag('ampm');
516  * </code>
517  *
518  * <code>
519  *  echo select_ampm_tag('ampm', 'PM', array('include_blank' => true));
520  * </code>
521  *
522  * @param  string field name
523  * @param  integer selected value (AM or PM)
524  * @param  array  additional HTML compliant <select> tag parameters
525  * @return string <select> tag populated with AM and PM options for use with 12-Hour time.
526  * @see select_time_tag, select datetime_tag
527  */
528 function select_ampm_tag($name, $value = null, $options = array(), $html_options = array())
529 {
530   if ($value === null)
531   {
532     $value = date('A');
533   }
534
535   $options = _parse_attributes($options);
536   $select_options = array();
537   _convert_include_custom_for_select($options, $select_options);
538
539   $select_options['AM'] = 'AM';
540   $select_options['PM'] = 'PM';
541
542   return select_tag($name, options_for_select($select_options, $value), $html_options);
543 }
544
545 /**
546  * Returns three <select> tags populated with hours, minutes, and optionally seconds.
547  *
548  * By default, the <i>$value</i> parameter is set to the current hour and minute. To override this, simply pass a valid time
549  * or a correctly formatted time array (see example) to the <i>$value</i> parameter. You can also set the <i>$value</i>
550  * parameter to null which will disable the <i>$value</i>, however this will only be useful if you pass 'include_blank' or
551  * 'include_custom' to the <i>$options</i> parameter. To include seconds to the result, use set the 'include_second' option in the
552  * <i>$options</i> parameter to true. <b>Note:</b> The <i>$name</i> parameter will automatically converted to array names.
553  * For example, a <i>$name</i> of "time" becomes:
554  * <samp>
555  *  <select name="time[hour]">...</select>
556  *  <select name="time[minute]">...</select>
557  *  <select name="time[second]">...</select>
558  * </samp>
559  * 
560  * <b>Options:</b>
561  * - include_blank  - Includes a blank <option> tag at the beginning of the string with an empty value.
562  * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
563  * - include_second - If set to true, includes the "seconds" select tag as part of the result.
564  * - second_step    - If set, the seconds will be incremented in blocks of X, where X = 'second_step'
565  * - minute_step    - If set, the minutes will be incremented in blocks of X, where X = 'minute_step'
566  * - 12hour_time    - If set to true, will return integers 1 through 12 instead of the default 0 through 23 as well as an AM/PM select box.
567  * - time_seperator - Includes a string of defined text between each generated select tag
568  * - ampm_seperator - Includes a string of defined text between the minute/second select box and the AM/PM select box
569  * 
570  * <b>Examples:</b>
571  * <code>
572  *  echo select_time_tag('time');
573  * </code>
574  *
575  * <code>
576  *  echo select_time_tag('date', '09:31');
577  * </code>
578  *
579  * <code>
580  *  $time = array('hour' => '15', 'minute' => 46, 'second' => 01);
581  *  echo select_time_tag('time', $time, array('include_second' => true, '12hour_time' => true));
582  * </code>
583  *
584  * @param  string field name (automatically becomes an array of parts: name[hour], name[minute], year[second])
585  * @param  mixed  accepts a valid time string or properly formatted time array
586  * @param  array  additional HTML compliant <select> tag parameters
587  * @return string three <select> tags populated with a hours, minutes and optionally seconds.
588  * @see select datetime_tag, select_hour_tag, select_minute_tag, select_second_tag
589  */
590 function select_time_tag($name, $value = null, $options = array(), $html_options = array())
591 {
592   $options = _parse_attributes($options);
593
594   $time_seperator = _get_option($options, 'time_seperator', ':');
595   $ampm_seperator = _get_option($options, 'ampm_seperator', '');
596   $include_second = _get_option($options, 'include_second');
597   $_12hour_time   = _get_option($options, '12hour_time');
598
599   $options['12hour_time'] = $_12hour_time; // set it back. hour tag needs it.
600
601   if ($include_custom = _get_option($options, 'include_custom'))
602   {
603     $include_custom_hour = (is_array($include_custom))
604         ? ((isset($include_custom['hour'])) ? array('include_custom'=>$include_custom['hour']) : array())
605         : array('include_custom'=>$include_custom);
606
607     $include_custom_minute = (is_array($include_custom))
608         ? ((isset($include_custom['minute'])) ? array('include_custom'=>$include_custom['minute']) : array())
609         : array('include_custom'=>$include_custom);
610
611     $include_custom_second = (is_array($include_custom))
612         ? ((isset($include_custom['second'])) ? array('include_custom'=>$include_custom['second']) : array())
613         : array('include_custom'=>$include_custom);
614
615     $include_custom_ampm = (is_array($include_custom))
616         ? ((isset($include_custom['ampm'])) ? array('include_custom'=>$include_custom['ampm']) : array())
617         : array('include_custom'=>$include_custom);
618   }
619   else
620   {
621     $include_custom_hour = array();
622     $include_custom_minute = array();
623     $include_custom_second = array();
624     $include_custom_ampm = array();
625   }
626
627   $tags = array();
628
629   $hour_name = $name.'[hour]';
630   $tags[] = select_hour_tag($hour_name, _parse_value_for_date($value, 'hour', $_12hour_time ? 'h' : 'H'), $options + $include_custom_hour, $html_options);
631
632   $minute_name = $name.'[minute]';
633   $tags[] = select_minute_tag($minute_name, _parse_value_for_date($value, 'minute', 'i'), $options + $include_custom_minute, $html_options);
634
635   if ($include_second)
636   {
637     $second_name = $name.'[second]';
638     $tags[] = select_second_tag($second_name, _parse_value_for_date($value, 'second', 's'), $options + $include_custom_second, $html_options);
639   }
640
641   $time = implode($time_seperator, $tags);
642
643   if ($_12hour_time)
644   {
645     $ampm_name = $name.'[ampm]';
646     $time .=  $ampm_seperator.select_ampm_tag($ampm_name, _parse_value_for_date($value, 'ampm', 'A'), $options + $include_custom_ampm, $html_options);
647   }
648
649   return $time;
650 }
651
652 /**
653  * Returns a variable number of <select> tags populated with date and time related select boxes.
654  *
655  * The select_datetime_tag is the culmination of both the select_date_tag and the select_time_tag.
656  * By default, the <i>$value</i> parameter is set to the current date and time. To override this, simply pass a valid
657  * date, time, datetime string or correctly formatted array (see example) to the <i>$value</i> parameter.
658  * You can also set the <i>$value</i> parameter to null which will disable the <i>$value</i>, however this
659  * will only be useful if you pass 'include_blank' or 'include_custom' to the <i>$options</i> parameter.
660  * To include seconds to the result, use set the 'include_second' option in the <i>$options</i> parameter to true.
661  * <b>Note:</b> The <i>$name</i> parameter will automatically converted to array names.
662  * For example, a <i>$name</i> of "datetime" becomes:
663  * <samp>
664  *  <select name="datetime[month]">...</select>
665  *  <select name="datetime[day]">...</select>
666  *  <select name="datetime[year]">...</select>
667  *  <select name="datetime[hour]">...</select>
668  *  <select name="datetime[minute]">...</select>
669  *  <select name="datetime[second]">...</select>
670  * </samp>
671  * 
672  * <b>Options:</b>
673  * - include_blank     - Includes a blank <option> tag at the beginning of the string with an empty value.
674  * - include_custom    - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
675  * - include_second    - If set to true, includes the "seconds" select tag as part of the result.
676  * - discard_month     - If set to true, will only return select tags for day and year.
677  * - discard_day       - If set to true, will only return select tags for month and year.
678  * - discard_year      - If set to true, will only return select tags for month and day.
679  * - use_month_numbers - If set to true, will show the month's numerical value (1 - 12) instead of the months full name.
680  * - use_short_month   - If set to true, will show the month's short name (i.e. Jan, Feb, Mar) instead of its full name.
681  * - year_start        - If set, the range of years will begin at this four-digit date (i.e. 1979)
682  * - year_end          - If set, the range of years will end at this four-digit date (i.e. 2025)
683  * - second_step       - If set, the seconds will be incremented in blocks of X, where X = 'second_step'
684  * - minute_step       - If set, the minutes will be incremented in blocks of X, where X = 'minute_step'
685  * - 12hour_time       - If set to true, will return integers 1 through 12 instead of the default 0 through 23.
686  * - date_seperator    - Includes a string of defined text between each generated select tag
687  * - time_seperator    - Includes a string of defined text between each generated select tag
688  * - ampm_seperator    - Includes a string of defined text between the minute/second select box and the AM/PM select box
689  * 
690  * <b>Examples:</b>
691  * <code>
692  *  echo select_datetime_tag('datetime');
693  * </code>
694  *
695  * <code>
696  *  echo select_datetime_tag('datetime', '1979-10-30');
697  * </code>
698  *
699  * <code>
700  *  $datetime = array('year' => '1979', 'month' => 10, 'day' => 30, 'hour' => '15', 'minute' => 46);
701  *  echo select_datetime_tag('time', $datetime, array('use_short_month' => true, '12hour_time' => true));
702  * </code>
703  *
704  * @param  string field name (automatically becomes an array of date and time parts)
705  * @param  mixed  accepts a valid time string or properly formatted time array
706  * @param  array  additional HTML compliant <select> tag parameters
707  * @return string a variable number of <select> tags populated with date and time related select boxes
708  * @see select date_tag, select_time_tag
709  */
710 function select_datetime_tag($name, $value = null, $options = array(), $html_options = array())
711 {
712   $options = _parse_attributes($options);
713   $datetime_seperator = _get_option($options, 'datetime_seperator', '');
714
715   $date = select_date_tag($name, $value, $options, $html_options);
716   $time = select_time_tag($name, $value, $options, $html_options);
717
718   return $date.$datetime_seperator.$time;
719 }
720
721 /**
722  * Returns a <select> tag, populated with a range of numbers
723  *
724  * By default, the select_number_tag generates a list of numbers from 1 - 10, with an incremental value of 1.  These values
725  * can be easily changed by passing one or several <i>$options</i>.  Numbers can be either positive or negative, integers or decimals,
726  * and can be incremented by any number, decimal or integer.  If you require the range of numbers to be listed in descending order, pass
727  * the 'reverse' option to easily display the list of numbers in the opposite direction.
728  *
729  * <b>Options:</b>
730  * - include_blank  - Includes a blank <option> tag at the beginning of the string with an empty value.
731  * - include_custom - Includes an <option> tag with a custom display title at the beginning of the string with an empty value.
732  * - multiple       - If set to true, the select tag will allow multiple numbers to be selected at once.
733  * - start          - The first number in the list. If not specified, the default value is 1.
734  * - end            - The last number in the list. If not specified, the default value is 10.
735  * - increment      - The number by which to increase each number in the list by until the number is greater than or equal to the 'end' option.
736  *                    If not specified, the default value is 1.
737  * - reverse        - Reverses the order of numbers so they are display in descending order
738  *
739  * <b>Examples:</b>
740  * <code>
741  *  echo select_number_tag('rating', '', array('reverse' => true));
742  * </code>
743  *
744  * <code>
745  *  echo echo select_number_tag('tax_rate', '0.07', array('start' => '0.05', 'end' => '0.09', 'increment' => '0.01'));
746  * </code>
747  *
748  * <code>
749  *  echo select_number_tag('limit', 5, array('start' => 5, 'end' => 120, 'increment' => 15));
750  * </code>
751  *
752  * @param  string field name
753  * @param  string the selected option
754  * @param  array  <i>$options</i> to manipulate the output of the tag.
755  * @param  array  additional HTML compliant <select> tag parameters
756  * @return string <select> tag populated with a range of numbers.
757  * @see options_for_select, content_tag
758  */
759 function select_number_tag($name, $value, $options = array(), $html_options = array())
760 {
761   $increment = _get_option($options, 'increment', 1);
762
763   $range = array();
764   $max = _get_option($options, 'end', 10) + $increment;
765   for ($x = _get_option($options, 'start', 1); $x < $max; $x += $increment)
766   {
767     $range[(string) $x] = $x;
768   }
769
770   if (_get_option($options, 'reverse'))
771   {
772     $range = array_reverse($range, true);
773   }
774
775   return select_tag($name, options_for_select($range, $value, $options), $html_options);
776 }
777
778 /**
779  * Returns a <select> tag populated with all the timezones in the world.
780  *
781  * The select_timezone_tag builds off the traditional select_tag function, and is conveniently populated with
782  * all the timezones in the world (sorted alphabetically). Each option in the list has a unique timezone identifier
783  * for its value and the timezone's locale as its display title.  The timezone data is retrieved via the sfCultureInfo
784  * class, which stores a wide variety of i18n and i10n settings for various countries and cultures throughout the world.
785  * Here's an example of an <option> tag generated by the select_timezone_tag:
786  *
787  * <b>Options:</b>
788  * - display -
789  *     identifer         - Display the PHP timezone identifier (e.g. America/Denver)
790  *     timezone          - Display the full timezone name (e.g. Mountain Standard Time)
791  *     timezone_abbr     - Display the timezone abbreviation (e.g. MST)
792  *     timzone_dst       - Display the full timezone name with daylight savings time (e.g. Mountain Daylight Time)
793  *     timezone_dst_abbr - Display the timezone abbreviation with daylight savings time (e.g. MDT)
794  *     city              - Display the city/region that relates to the timezone (e.g. Denver)
795  *
796  * <samp>
797  *  <option value="America/Denver">America/Denver</option>
798  * </samp>
799  *
800  * <b>Examples:</b>
801  * <code>
802  *  echo select_timezone_tag('timezone', 'America/Denver');
803  * </code>
804  *
805  * @param  string field name
806  * @param  string selected field value (timezone identifier)
807  * @param  array  additional HTML compliant <select> tag parameters
808  * @return string <select> tag populated with all the timezones in the world.
809  * @see select_tag, options_for_select, sfCultureInfo
810  */
811 function select_timezone_tag($name, $selected = null, $options = array())
812 {
813   if (!isset($options['display'])) $options['display'] = 'identifier';
814  
815   $c = new sfCultureInfo(sfContext::getInstance()->getUser()->getCulture());
816   $timezone_groups = $c->getTimeZones();
817
818   $display_key = 0;
819
820   switch ($options['display'])
821   {
822     case "identifier":
823       $display_key = 0;
824       break;
825
826     case "timezone":
827       $display_key = 1;
828       break;
829
830     case "timezone_abbr":
831       $display_key = 2;
832       break;
833
834     case "timezone_dst":
835       $display_key = 3;
836       break;
837
838     case "timezone_dst_abbr":
839       $display_key = 4;
840       break;
841
842     case "city":
843       $display_key = 5;
844       break;
845
846     default:
847       $display_key = 0;
848       break;
849   }
850
851   unset($options['display']);
852
853   $timezones = array();
854   foreach ($timezone_groups as $tz_group_key => $tz_group)
855   {
856     $array_key = null;
857
858     foreach ($tz_group as $tz_key => $tz)
859     {
860       if ($tz_key == 0) $array_key = $tz;
861       if ($tz_key == $display_key AND !empty($tz)) $timezones[$array_key] = $tz;
862     }
863   }
864
865   if ($timezone_option = _get_option($options, 'timezones'))
866   {
867     $diff = array_diff_key($timezones, array_flip((array) $timezone_option));
868     foreach ($diff as $key => $v)
869     {
870       unset($timezones[$key]);
871     }
872   }
873
874   // Remove duplicate values
875   $timezones = array_unique($timezones);
876
877   asort($timezones);
878
879   $option_tags = options_for_select($timezones, $selected);
880
881   return select_tag($name, $option_tags, $options);
882 }
883
884 /**
885  * Converts date values (<i>$value</i>) into its correct date format (<i>$format_char</i>)
886  *
887  * This function is primarily used in select_date_tag, select_time_tag and select_datetime_tag.
888  *
889  * <b>Note:</b> If <i>$value</i> is empty, it will be populated with the current date and time.
890  *
891  * @param  string date or date part
892  * @param  string custom key for array values
893  * @return string properly formatted date part value.
894  * @see select_date_tag, select_time_tag, select_datetime_tag
895  */
896 function _parse_value_for_date($value, $key, $format_char)
897 {
898   if (is_array($value))
899   {
900     return (isset($value[$key])) ? $value[$key] : '';
901   }
902   else if (is_numeric($value))
903   {
904     return date($format_char, $value);
905   }
906   else if ($value == '' || ($key == 'ampm' && ($value == 'AM' || $value == 'PM')))
907   {
908     return $value;
909   }
910   else if (empty($value))
911   {
912     $value = date('Y-m-d H:i:s');
913   }
914
915   // english text presentation
916   return date($format_char, strtotime($value));
917 }
918
919 /**
920  * Retrieves the proper date format based on the specified <i>$culture</i> setting
921  *
922  * <b>Note:</b> If no <i>$culture</i> is defined, the user's culture setting will be used in its place.
923  *
924  * @param  string two or three character culture setting variable
925  * @return string formatted date/time format based on the specified date/time setting
926  * @see sfUser
927  */
928 function _get_I18n_date_locales($culture = null)
929 {
930   if (!$culture)
931   {
932     $culture = sfContext::getInstance()->getUser()->getCulture();
933   }
934
935   $retval = array('culture'=>$culture);
936
937   $dateFormatInfo = sfDateTimeFormatInfo::getInstance($culture);
938   $date_format = strtolower($dateFormatInfo->getShortDatePattern());
939
940   $retval['dateFormatInfo'] = $dateFormatInfo;
941
942   $match_pattern = "/([dmy]+)(.*?)([dmy]+)(.*?)([dmy]+)/";
943   if (!preg_match($match_pattern, $date_format, $match_arr))
944   {
945     // if matching fails use en shortdate
946     preg_match($match_pattern, 'm/d/yy', $match_arr);
947   }
948
949   $retval['date_seperator'] = $match_arr[2];
950
951   // unset all but [dmy]+
952   unset($match_arr[0], $match_arr[2], $match_arr[4]);
953
954   $retval['date_order'] = array();
955   foreach ($match_arr as $v)
956   {
957     // 'm/d/yy' => $retval[date_order] = array ('m', 'd', 'y');
958     $retval['date_order'][] = $v[0];
959   }
960
961   return $retval;
962 }
963
Note: See TracBrowser for help on using the browser.