<?php
$showtimes = [
"2014-01-20 20:00:00",
"2014-01-21 20:00:00",
"2014-01-22 14:00:00",
"2014-01-22 20:00:00",
"2014-01-23 20:00:00",
"2014-01-25 20:00:00",
"2014-01-26 20:00:00",
"2014-01-31 20:00:00",
"2014-02-01 20:00:00"
];
// 20th-23rd Jan 8pm, 22nd Jan 2pm, 25th-26th Jan 8pm, 31st Jan - 1st Feb 8pm
foreach ($showtimes as $showtime) {
$dt = new DateTime($showtime);
$thisDay = $dt->format('Y-m-d H:i:s');
$nextDay = (clone $dt)->modify('+1 day')->format('Y-m-d H:i:s');
if (!isset($ref[$thisDay])) {
$ref[$nextDay] = $dt->format('jS M ga');
$result[] =& $ref[$nextDay];
continue;
}
// if same month
if (str_contains($ref[$thisDay], $dt->format('M'))) {
// inject day
$ref[$thisDay] = preg_replace(
'/[tdh]\b\K[^ ]*/',
$dt->format('-jS'),
$ref[$thisDay]
);
} else {
// inject day and month
$ref[$thisDay] = preg_replace(
'/ (?=[^ ]*$)/',
$dt->format(' - jS M '),
$ref[$thisDay]
);
}
$ref[$nextDay] =& $ref[$thisDay];
}
echo implode(', ', $result);
preferences:
47.24 ms | 406 KiB | 5 Q