"{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($options['formats']) && is_array($options['formats'])) { $formats = array_merge($formats, $options['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($start); if (empty($end)) { return $formatDate($formats['oneDay'], $startTime); } $endTime = strtotime($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); } public static function stripTags($string) { $string = strip_tags($string); $string = preg_replace("/[\n\r]+/", "\n", $string); $string = preg_replace("/\s+/", " ", $string); $string = html_entity_decode($string, ENT_COMPAT | ENT_QUOTES | ENT_HTML401); return $string; } }