"{m1} {d1}, {y1}", // Oct. 1, 2013 'oneMonth' => "{m1} {d1}-{d2}, {y1}", // Oct. 1-20, 2013 'oneYear' => "{m1} {d1} - {m2} {d2}, {y1}", // Oct. 1 - Dec. 20, 2013 'multiYears' => "{m1} {d1}, {y1} - {m2} {d2}, {y2}", // Oct. 1, 2013 - Dec. 20, 2014 ); if (!empty($params['formats']) && is_array($params['formats'])) { $formats = array_merge($formats, $params['formats']); } $getDate = function($time, $index) use ($monthFormat, $dayFormat, $yearFormat) { return [ "{m{$index}}" => strftime($monthFormat, $time), "{d{$index}}" => trim(strftime($dayFormat, $time)), "{y{$index}}" => strftime($yearFormat, $time), ]; }; $formatDate = function ($format, $startTime, $endTime = null) use ($getDate) { $map = $getDate($startTime, 1); if ($endTime) { $map = array_merge($map, $getDate($endTime, 2)); } return str_replace(array_keys($map), array_values($map), $format); }; $startTime = strtotime($params['start']); if (empty($params['end'])) { return $formatDate($formats['oneDay'], $startTime); } $endTime = strtotime($params['end']); if (idate('Y', $startTime) == idate('Y', $endTime)) { if (idate('m', $startTime) == idate('m', $endTime)) { if (idate('d', $startTime) == idate('d', $endTime)) { return $formatDate($formats['oneDay'], $startTime, $endTime); } else { return $formatDate($formats['oneMonth'], $startTime, $endTime); } } else { return $formatDate($formats['oneYear'], $startTime, $endTime); } } return $formatDate($formats['multiYears'], $startTime, $endTime); }