Last active
June 1, 2022 10:01
-
-
Save ayhanmalkoc/ec9e23affbe5a777c2ebd06384134176 to your computer and use it in GitHub Desktop.
Create display date range from two dates in ACF in Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$start_date = strtotime (get_field('start_date')); | |
$end_date = strtotime (get_field('end_date')); | |
if ( $start_date == $end_date ){ | |
// the start and end are equal "d F Y" #1 | |
echo date_i18n( "j F, l Y", $start_date ); | |
} else { | |
// first check to see if the year is the same since it is a larger scope | |
if ( date( 'Y', $start_date ) == date( 'Y', $end_date ) ){ | |
// year is the same, check the month next | |
if ( date( 'M', $start_date ) == date( 'M', $end_date ) ){ | |
// month is the same, use "d - D F Y", #2 | |
echo date_i18n( 'd', $start_date ) . ' - ' . date_i18n( 'd F, Y', $end_date ); | |
} else { | |
// month is not the same but year is a match, use "d F - d F Y", #3 | |
echo date_i18n( 'd F', $start_date ) . ' - ' . date_i18n( 'd F Y', $end_date ); | |
} | |
} else { | |
// the year is not the same, use "d F Y - d F Y", #4 | |
echo date_i18n( 'd F Y', $start_date ) . ' - ' . date_i18n( 'd F Y', $end_date ); | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment