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) {
41 if ($hasFrom && !$hasTo) {
42 $params[
'to'] = $params[
'from'];
44 elseif (!$hasFrom && $hasTo) {
45 $params[
'from'] = $params[
'to'];
48 $from =
new DateTime($params[
'from']);
49 $to =
new DateTime($params[
'to']);
51 $displayTime = isset($params[
'displaytime']) ? $params[
'displaytime'] :
false;
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');
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]);
64 $fullFormat = $formats[0];
65 $monthFormat = $formats[1];
66 $dayFormat = $formats[2];
68 $delim = isset($params[
'delim']) ? $params[
'delim'] :
' – ';
70 $formatType = isset($params[
'formattype']) ? $params[
'formattype'] :
'auto';
71 if ($formatType ==
'auto') {
72 $formatType = strpos(join(
'', $formats),
'%') ===
false ?
'date' :
'strftime';
75 $formatFunction =
function(DateTime $date, $format) use ($formatType) {
76 return $formatType ==
'date' ? $date->format($format) : strftime($format, $date->getTimestamp());
80 if ($sameDay && $sameTime) {
81 $result = $formatFunction($from, $fullFormat);
84 $result = $formatFunction($from, $fullFormat).$delim.$formatFunction($to, $dayFormat);
87 $result = $formatFunction($from, $fullFormat).$delim.$formatFunction($to, $fullFormat);
92 $result = $formatFunction($from, $fullFormat);
95 $result = $formatFunction($from, $dayFormat).$delim.$formatFunction($to, $fullFormat);
98 $result = $formatFunction($from, $monthFormat).$delim.$formatFunction($to, $fullFormat);
101 $result = $formatFunction($from, $fullFormat).$delim.$formatFunction($to, $fullFormat);