Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
function the_date_range($args) {
global $post;
$default = array(
'start_field' => 'start_date',
'end_field' => null,
'base_format' => 'Ymd',
'post_id' => $post->ID,
'separator' => '<span class="date-separator">&ndash;</span>',
'month_format' => 'F',
'day_format' => 'j',
'year_format' => 'Y'
$s = array_intersect_key($args + $default, $default);
$start = get_field($s['start_field'], $s['post_id']);
$end = get_field($s['end_field'], $s['post_id']);
// Checks to make sure the start is a valid field
if($start) {
$raw_dates['start'] = DateTime::createFromFormat( $s['base_format'], $start );
} else {
// Adds end field if there is one
if( $end ) {
$raw_dates['end'] = DateTime::createFromFormat( $s['base_format'], $end );
// Sets up the $dates array
foreach($raw_dates as $key => $value) {
$dates[$key] = array(
'month' =>$value->format($s['month_format']),
'day' => $value->format($s['day_format']),
'year' => $value->format($s['year_format'])
// if the years aren't the same the whole output has to change so we check that
// at the beginning
if($dates['start']['year'] == $dates['end']['year']) {
// if years are the same and months are the same
if($dates['start']['month'] == $dates['end']['month']) {
// if years, months and days are the same (same date in both fields)
if($dates['start']['day'] == $dates['end']['day']) {
$range = $dates['start']['month']." ".$dates['start']['day'].", ".$dates['start']['year'];
// if years and months are the same but not days
} else {
$range = $dates['start']['month']." ".$dates['start']['day'].$s['separator'].$dates['end']['day'].", ".$dates['start']['year'];
// if years are the same but months are not the same
} else {
$range = $dates['start']['month']." ".$dates['start']['day'].$s['separator'].$dates['end']['month']." ".$dates['end']['day'].", ".$dates['start']['year'];
} else {
$range = $dates['start']['month']." ".$dates['start']['day'].", ".$dates['start']['year'].$s['separator'].$dates['end']['month']." ".$dates['end']['day'].", ".$dates['end']['year'];
echo $range;
Copy link

dohnutt commented Dec 12, 2016

This is fantastic! I'll def be using this

Copy link

strarsis commented Feb 12, 2017

Could you move this into a Github repository with a composer.json?
Edit: Please also add a License!

Copy link

seldimi commented May 15, 2017

Will that add a New Field in ACF on admin or not? Any documentation on where to add it ?

Copy link

stevengrimaldo commented Nov 22, 2018

I added this as is to my functions.php file and it literally did nothing, i see no additional options when creating a custom field using ACF and i don't see any additional features when using the regular date field with ACF.

Copy link

OwaisDG commented Jan 19, 2019

not working

Copy link

marianagarciaferreira commented Jan 31, 2019

not work

Copy link

atazminhlk commented Feb 11, 2019

Worked great, thank you.

In Template

In functions.php
I replaced 2 lines
'start_field' => 'callout_text_details_date_start',
'end_field' => 'callout_text_details_date_end',

callout_text_details_date_start and callout_text_details_date_end
are ACF field names, return format Ymd (e.g. 20190210)

Copy link

mysticbounce commented May 29, 2019

I wish I could get this working as it seems to be for some others.

I tried passing the two separate ‘date picker’ fields as the arguments but this was showing the following error Warning: A non-numeric value encountered so I tried passing the_field('field-name') which at least shows removed that error as a string was being passed to the function.

However, the following error always persists:

Fatal error: Uncaught Error: Unsupported operand types in relation to $s = array_intersect_key($args + $default, $default);. The fields are both setup to return Ymd formatting as shown in the $default array();.

Any idea’s as too what I may be doing wrong would be much appreciated.

Copy link

keishabien commented Oct 17, 2022

Hi - where is this code supposed to echo out to? I have it in my functions file but cannot find the results on the back or front end.

Copy link

brianjhanson commented Oct 17, 2022

@keishabien it won't output anything by default, you'd need to call the_date_range($args) in your template, where $args is an array of values like this.

That being said, as others reported this snippet probably doesn't work anymore. Sorry everyone, I somehow missed that people were trying to use it and haven't kept it updated at all.

Copy link

keishabien commented Oct 17, 2022

Thanks for the quick reply Brian! I will test out what you mentioned and see if I can get that to work. It's interesting that ACF still doesn't seem to have this feature natively. Cheers!

Copy link

keishabien commented Oct 17, 2022

@brianjhanson hi again! I correctly called the function and was able to get it to display my date range. Thanks again for replying!

@atazminhlk 's answer was also key to my solution - setting the field names and base format correctly. The ACF field return format should match the base_format in the $args array.

Copy link

brianjhanson commented Oct 17, 2022

@keishabien 🎉 glad you got it working!

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