Home › Forums › General Issues › Extract and compare month from two date time picker fields › Reply To: Extract and compare month from two date time picker fields
I’ve recently needed to do something similar, also don’t forget that you could have something that run from December of one year to January of the following year.
I don’t use DateTime
because I feel it just over complicates things, but here is the code I used for that site.
$this_year = date('Y');
$start_date = get_field('start_date');
$start_time = strtotime($start_date);
$start_year = date('Y', $start_time);
$start_month = date('F', $start_time);
$start_day = date('j', $start_time);
$end_date = get_field('end_date');
$end_time = strtotime($end_date);
$end_year = date('Y', $end_time);
$end_month = date('F', $end_time);
$end_day = date('j', $end_time);
if ($end_year != $start_year) {
$display_date = $start_month.' '.$start_day.', '.$start_year.' - '.$end_month.' '.$end_day.', '.$end_year;
} elseif ($end_month != $start_month) {
$display_date = $start_month.' '.$start_day.' - '.$end_month.' '.$end_day;
if ($start_year != $this_year) {
$display_date .= ', '.$start_year;
}
} elseif ($end_day != $start_day) {
$display_date = $start_month.' '.$start_day.' - '.$end_day;
if ($start_year != $this_year) {
$display_date .= ', '.$start_year;
}
} else {
$display_date = $start_month.' '.$start_day;
if ($start_year != $this_year) {
$display_date .= ', '.$start_year;
}
}
echo $display_date;
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.