ACF, Date/Time, PHP | Type: PHPDisplay Dates if Start and End Dates Same Month

Handy snippet for displaying event dates when events can stretch over multiple days. ACF date field is set to return ‘Ymd’ strings (eg, 20250315)

$start-date = get_field('start_date'); //load string (use in comparisons)
$end-date = get_field('end_date'); //load string (use in comparisons)
$startdate = DateTime::createFromFormat( 'Ymd', $start-date ); //convert string to DateTime object
$endate = DateTime::createFromFormat( 'Ymd', $end-date ); //convert string to DateTime object
//output dates in custom formats
if (!($start-date == $end-date)) { //if not same date (compare strings)
    if ($startdate->format('m') == $endate->('m')) { //if same month (lowercase 'm' is 2-digit number)
        echo $startdate->format('F j');
        echo '-';
        echo $endate->format('j, Y');
    } else { //if not same month
        echo $startdate->format('M j');
        echo '-';
        echo $endate->format('M j, Y');
    }
} elseif ($startdate) { // start date and end date are the same date
    echo $startdate->format('D., F j, Y');
}