function.daterange.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 
12 /**
13  * Format one or two dates avoiding duplicate years and months (e.g. 10. - 11.03.2016).
14  *
15  * Example:
16  * @code
17  * {daterange from='2018-01-01' to='2018-03-01' lang='de'}
18  * @endcode
19  *
20  * @param $params Array with keys:
21  * - from: Start date in a format that can be parsed into a DateTime instance
22  * - to: End date in a format that can be parsed into a DateTime instance
23  * - formats: Array of maximum 3 date formats (day-month-year, day-month, day) to use, wich will be used to format
24  * the first date according to the relation to the second date which uses the full format (optional, default: ['d.m.Y', 'd.m.', 'd.'])
25  * If displaytime is true, the default formats are ['d.m.Y H:i', 'd.m.Y H:i', 'H:i'] and the format is used for the second date
26  * If less than 3 formats are specified, last format in the list will be used for unspecified formats
27  * - delim: Delimiter string to be used to separate the values (optional, default: ' – ' (&ndash;))
28  * - formattype: Format type used in formats parameter (optional, 'strftime'|'date'|'auto', default: 'auto')
29  * - displaytime: Also take time into account (optional, default: false)
30  * @note The automatic decision for a format type is based on the existance of a % char in the formats parameter.
31  * @param $template Smarty_Internal_Template
32  * @return String
33  */
34 function smarty_function_daterange(array $params, Smarty_Internal_Template $template) {
35  $hasFrom = strlen($params['from']) > 0;
36  $hasTo = strlen($params['to']) > 0;
37  if (!$hasFrom && !$hasTo) {
38  return '';
39  }
40 
41  if ($hasFrom && !$hasTo) {
42  $params['to'] = $params['from'];
43  }
44  elseif (!$hasFrom && $hasTo) {
45  $params['from'] = $params['to'];
46  }
47 
48  $from = new DateTime($params['from']);
49  $to = new DateTime($params['to']);
50 
51  $displayTime = isset($params['displaytime']) ? $params['displaytime'] : false;
52 
53  $sameDay = $from->format('Y-m-d') == $to->format('Y-m-d');
54  $sameMonth = $from->format('Y-m') == $to->format('Y-m');
55  $sameYear = $from->format('Y') == $to->format('Y');
56  $sameTime = $from->format('H:i') == $to->format('H:i');
57 
58  $formats = !$displayTime ? ['d.m.Y', 'd.m.', 'd.'] : ['d.m.Y H:i', 'd.m.Y H:i', 'H:i'];
59  if (isset($params['formats']) && is_array($params['formats'])) {
60  $userFormats = $params['formats'];
61  $numFormats = sizeof($userFormats);
62  $formats = $numFormats >= 3 ? array_slice($userFormats, 0, 3) : array_pad($userFormats, 3, $userFormats[$numFormats-1]);
63  }
64  $fullFormat = $formats[0];
65  $monthFormat = $formats[1];
66  $dayFormat = $formats[2];
67 
68  $delim = isset($params['delim']) ? $params['delim'] : ' – ';
69 
70  $formatType = isset($params['formattype']) ? $params['formattype'] : 'auto';
71  if ($formatType == 'auto') {
72  $formatType = strpos(join('', $formats), '%') === false ? 'date' : 'strftime';
73  }
74 
75  $formatFunction = function(DateTime $date, $format) use ($formatType) {
76  return $formatType == 'date' ? $date->format($format) : strftime($format, $date->getTimestamp());
77  };
78 
79  if ($displayTime) {
80  if ($sameDay && $sameTime) {
81  $result = $formatFunction($from, $fullFormat);
82  }
83  elseif ($sameDay) {
84  $result = $formatFunction($from, $fullFormat).$delim.$formatFunction($to, $dayFormat);
85  }
86  else {
87  $result = $formatFunction($from, $fullFormat).$delim.$formatFunction($to, $fullFormat);
88  }
89  }
90  else {
91  if ($sameDay) {
92  $result = $formatFunction($from, $fullFormat);
93  }
94  elseif ($sameMonth) {
95  $result = $formatFunction($from, $dayFormat).$delim.$formatFunction($to, $fullFormat);
96  }
97  elseif ($sameYear) {
98  $result = $formatFunction($from, $monthFormat).$delim.$formatFunction($to, $fullFormat);
99  }
100  else {
101  $result = $formatFunction($from, $fullFormat).$delim.$formatFunction($to, $fullFormat);
102  }
103  }
104  return $result;
105 }
106 ?>