Skip to content

Instantly share code, notes, and snippets.

@robdecker
Last active November 2, 2019 04:05
Show Gist options
  • Save robdecker/6694319 to your computer and use it in GitHub Desktop.
Save robdecker/6694319 to your computer and use it in GitHub Desktop.
[Format 2 dates into a range string] #php
<?php
/**
* Format start/end dates of a range for printing to the screen.
* ex: "April 11 - 18, 2011" or "December 29, 2011 - January 3, 2012"
*
* @param DateTime $start : start of the range
* @param DateTime $end : end of the range
* @return string : the formatted string
*/
function mnnshow_format_date_range($start, $end){
if ($start->format('Y') == $end->format('Y') && $start->format('m') == $end->format('m')) {
return $start->format('F j - ').$end->format('j, ').$start->format('Y');
}
elseif ($start->format('Y') == $end->format('Y')) {
return $start->format('F j - ').$end->format('F j, ').$start->format('Y');
}
else {
return $start->format('F j, Y - ').$end->format('F j, Y');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment