Skip to content

Instantly share code, notes, and snippets.

@trovster
Created March 30, 2012 16:57
Show Gist options
  • Select an option

  • Save trovster/2252907 to your computer and use it in GitHub Desktop.

Select an option

Save trovster/2252907 to your computer and use it in GitHub Desktop.
A PHP function to output start and end dates, allowing for for short syntax March 3rd-10th 2012 (if in same month & year).
<?php
/**
* event_output_start_end_date
* @param string $start_date
* @param string $end_date
* @return string
*/
function event_output_start_end_date($start_date, $end_date) {
$start_datetime = strtotime($start_date); // yyyy-mm-dd
$end_datetime = strtotime($end_date); // yyyy-mm-dd
$output = '';
if($start_datetime === $end_datetime) {
// same day - 1st April 2012
$output = date('jS F Y', $start_datetime);
}
elseif(date('Y-m', $start_datetime) === date('Y-m', $end_datetime)) {
// same year and month - 3rd - 21st March 2012
$output = sprintf('%s - %s %s', date('jS', $start_datetime), date('jS', $end_datetime), date('F Y', $start_datetime));
}
elseif(date('Y', $start_datetime) === date('Y', $end_datetime)) {
// same year - 29th January - 2nd February 2012
$output = sprintf('%s - %s %s', date('jS F', $start_datetime), date('jS F', $end_datetime), date('Y', $start_datetime));
}
else {
// completely different - 8th December 2012 - 2nd Janurary 2013
$output = sprintf('%s - %s', date('jS F Y', $start_datetime), date('jS F Y', $end_datetime));
}
return $output;
}
@markjames
Copy link

Bit late, but hopefully useful.

  • Allows config of day/month formatting
  • Handles php DateTime objects
  • Doesn't include year part if its in the same (YMMV with this bit!)
  • Uses ndashes for range separation
/**
 * Returns the shortest possible date range between the date and a
 * second date object (or date string/timestamp).
 * 
 * This method will build up the minimum required string to fully convey the
 * dates, eg. it will not duplicate the month name if both dates start and end
 * in the same month.
 *
 * @param mixed $start_date The start date as a timestamp, string or Date object
 * @param mixed $end_date The start date as a timestamp, string or Date object
 * @param string $month_format The php date() format to use for the month (either F or M)
 * @return string formatted end date
 */
public function NiceRange($start_date, $end_date, $day_format = "jS", $month_format = 'F') {

    // If we have been passed date objects or strings, convert to timestamps
    if( $start_date instanceof DateTime ) {
        $start_date = $start_date->getTimestamp();
    } elseif( !is_numeric( $start_date ) ) {
        $start_date = strtotime($start_date);
    }
    if( $end_date instanceof DateTime ) {
        $end_date = $end_date->getTimestamp();
    } elseif( !is_numeric( $end_date ) ) {
        $end_date = strtotime($end_date);
    }

    // Is start_date after end_date?
    if( $start_date > $end_date ) {
        $prev_start_date = $start_date;
        $start_date = $end_date;
        $end_date = $prev_start_date;
        unset($prev_start_date);
    }

    $is_same_day = date('Y-m-d',$start_date) == date('Y-m-d',$end_date);
    $is_same_month = $is_same_day || date('Y-m',$start_date) == date('Y-m',$end_date);
    $is_same_year = $is_same_day || $is_same_month || (date('Y',$start_date) == date('Y',$end_date));
    $is_current_year = (date('Y',$start_date) == date('Y')) && (date('Y',$end_date) == date('Y'));

    if( $is_same_day && $is_current_year ) {
        // Single day event in the current year
        return date("$day_format $month_format",$start_date);
    } elseif( $is_same_month && $is_current_year ) {
        // Current year, but a span of dates in a single month
        if( $end_date < time() ) {
            return date($day_format,$start_date) . '–' . date("$day_format $month_format Y",$end_date);
        } else {
            return date($day_format,$start_date) . '–' . date("$day_format $month_format",$end_date);
        }
    } elseif( $is_current_year ) {
        // Current year, but a span of dates
        if( $end_date < time() ) {
            return date("$day_format $month_format",$start_date) . '–' . date("$day_format $month_format Y",$end_date);
        } else {
            return date("$day_format $month_format",$start_date) . '–' . date("$day_format $month_format",$end_date);
        }
    } elseif( $is_same_day ) {
        // Single day event, not the current year
        return date("$day_format $month_format Y",$start_date);
    } elseif( $is_same_month ) {
        // Span of dates in a single month (not in current year)
        return date($day_format,$start_date) . '–' . date("$day_format $month_format",$end_date);
    } elseif( $is_same_year ) {
        // Span of dates where it starts in the same year that it ends
        return date("$day_format $month_format",$start_date) . '–' . date("$day_format $month_format Y",$end_date);
    } else {
        // Span of dates where it starts in a different year than it ends
        return date("$day_format $month_format Y",$start_date) . '–' . date("$day_format $month_format Y",$end_date);
    }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment