Skip to content

Instantly share code, notes, and snippets.

@markhowellsmead
Last active September 25, 2019 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markhowellsmead/8a3e64f71ff3108bfc8bed8c4083db87 to your computer and use it in GitHub Desktop.
Save markhowellsmead/8a3e64f71ff3108bfc8bed8c4083db87 to your computer and use it in GitHub Desktop.
Convert one or two dates (date from and date to) into a sensible, legible date string
/**
* Convert one or two dates (date from and date to) into a sensible, legible date string
* @param array $dates Array containing 'date_from' and (optionally) 'date_to'
* @return string Human-readable date text
*/
public function courseEntryDate(array $dates)
{
$dates['date_from'] = $dates['day'];
$dates['date_to'] = $dates['day_to'];
$date_from = $dates['date_from'];
if (empty($date_from)) {
return '';
}
$date_to = $dates['date_to'] ?? '';
$ts_date_from = strtotime($date_from);
if (empty($date_to)) {
return sprintf(
__('am %s', 'sha'),
date_i18n('d. F Y', $ts_date_from)
);
}
$ts_date_to = strtotime($date_to);
if (date_i18n('Ymd', $ts_date_from) == date_i18n('Ymd', $ts_date_to)) {
// Same day
return sprintf(
__('am %s', 'sha'),
date_i18n('d. F Y', strtotime($date_from))
);
}
if (date_i18n('Ym', $ts_date_from) == date_i18n('Ym', $ts_date_to)) {
// Same month
return sprintf(
__('vom %1$s bis %2$s', 'sha'),
date_i18n('d.', strtotime($date_from)),
date_i18n('d. F Y', strtotime($date_to))
);
}
if (date_i18n('Y', $ts_date_from) == date_i18n('Y', $ts_date_to)) {
// Same year
return sprintf(
__('vom %1$s bis %2$s', 'sha'),
date_i18n('d. F', strtotime($date_from)),
date_i18n('d. F Y', strtotime($date_to))
);
}
return sprintf(
__('vom %1$s bis %2$s', 'sha'),
date_i18n('d. F Y', strtotime($date_from)),
date_i18n('d. F Y', strtotime($date_to))
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment