Created
May 11, 2018 15:09
-
-
Save morgyface/e3580f4ddaedd2ac89a5f9c0abc6b153 to your computer and use it in GitHub Desktop.
WordPress | ACF | Simple date range
This file contains hidden or 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 | |
| function date_span_filter($start_date, $end_date) { | |
| if ( $start_date ) { | |
| // Dates should both be in a Y-m-d format | |
| $start_calendar_date = date('j F Y', strtotime($start_date)); | |
| if ( ! $end_date ) { | |
| // There is no range. Just return the required start date | |
| $date = $start_calendar_date; | |
| } else { | |
| if ( $start_date == $end_date ) { | |
| // We have two dates but they are the same so let us leave the end date alone for now | |
| $date = $start_calendar_date; | |
| } else { | |
| // We have a genuine date range so let us format and disassemble them | |
| $end_calendar_date = date('j F Y', strtotime($end_date)); | |
| $start_year = date('Y', strtotime($start_date)); // A full numeric representation of a year, 4 digits | |
| $start_month = date('F', strtotime($start_date)); // A full textual representation of a month | |
| $start_day = date('j', strtotime($start_date)); // Day of the month without leading zeros | |
| $end_year = date('Y', strtotime($end_date)); | |
| $end_month = date('F', strtotime($end_date)); | |
| $end_day = date('j', strtotime($end_date)); | |
| if ( $start_year != $end_year ) { | |
| // The years are different therefore they are in different months and days | |
| $date = $start_calendar_date . ' - ' . $end_calendar_date; | |
| } elseif ( $start_month != $end_month ) { | |
| // The years are the same but the months are different | |
| $date = $start_day . ' ' . $start_month . ' - ' . $end_day . ' ' . $end_month . ' ' . $end_year; | |
| } else { | |
| // The years are the same, the months are the same but the days are different | |
| $date = $start_day . ' - ' . $end_day . ' ' . $end_month . ' ' . $end_year; | |
| } | |
| } | |
| } | |
| return $date; | |
| } | |
| return null; | |
| } | |
| ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Date range
Removes repetition of months and years in date ranges.
For example; 4 May 2019 - 6 May 2019, simply becomes 4 - 6 May 2019.
If you want something with fancy formatting, suffixes and superscript tags, have a look at my pretty date range gist.