I have two Advanced Custom Fields datepicker fields in Wordpress – a starting date and a finishing date. These are in the back end, so that my client can add/edit posts and give them a date range.
Dates below are given as dd/mm/yyyy (I'm Australian).
I need to compare the two dates to come up with a nice date range for display on the front end. It needs to:
Check to see whether the two dates are the same, in which case it should just output one date (e.g. start date is 20/7/2015, finish date is 20/7/2015, so output is 20 July 2015).
If the two dates are different but in the same month, output as dd – dd mm yyyy (e.g. start date is 10/3/2015, finish date is 25/3/2015, so output is 10 – 25 March 2015).
If the two dates are in different months but the same year, output as dd mm – dd mm yyyy (e.g. start date is 18/4/2015, finish date is 3/5/2015, so output is 18 April – 3 May 2015).
Finally, if the two dates are in different years, output as dd mm yyyy – dd mm yyyy (e.g. start date is 8/11/2015, finish date is 6/2/2016, so output is 8 November 2015 – 6 February 2016).
I can do 1 and 4 but lack the coding skills to manage 2 or 3.
I'm very surprised I couldn't find this question on SO, so if I've overlooked a duplicate please point me in the right direction.
Thanks.
You can use the strtotime()
function to convert your date string from the meta values to a timestamp, which you can then use to compare. The timestamp can then be passed to the date()
function to compare the different date parts, and eventually to format the return value.
$start_date = strtotime( '20/7/2015' );
$finish_date = strtotime( '20/8/2015' );
if ( $start_date == $finish_date ){
// the start and finish are equal "d F Y" #1
return date( 'd F 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', $finish_date ) ){
// year is the same, check the month next
if ( date( 'M', $start_date ) == date( 'M', $finish_date ) ){
// month is the same, use "d - D F Y", #2
return date( 'd', $start_date ) . ' - ' . date( 'd F Y', $finish_date );
} else {
// month is not the same but year is a match, use "d F - d F Y", #3
return date( 'd F', $start_date ) . ' - ' . date( 'd F Y', $finish_date );
}
} else {
// the year is not the same, use "d F Y - d F Y", #4
return date( 'd F Y', $start_date ) . ' - ' . date( 'd F Y', $finish_date );
}
}
<?php
$start_date=strtotime('25-5-2010');
$end_date = strtotime('23-7-2012');
if(strcmp($start_date,$end_date)==0){
$newDate = date('d F Y', ($start_date));
echo $newDate;
}
elseif((strcmp(date('d',$start_date),date('d',$end_date))==0) && (strcmp(date('Y',$start_date),date('Y',$end_date))==0) && (strcmp(date('n',$start_date),date('n',$end_date))!==0)){
$newDate = date('d', ($start_date));
$newDate2 = date('Y', ($start_date));
echo $newDate.' '.date('F',$start_date).'-'.date('F',$end_date).' '.$newDate2;
}
elseif((strcmp(date('n',$start_date),date('n',$end_date))==0) && (strcmp(date('Y',$start_date),date('Y',$end_date))==0) && (strcmp(date('d',$start_date),date('d',$end_date))!==0)){
$newDate = date('F Y', ($start_date));
echo date('d',$start_date).'-'.date('d',$end_date).' '.$newDate;
}
elseif((strcmp(date('n',$start_date),date('n',$end_date))!==0) && (strcmp(date('Y',$start_date),date('Y',$end_date))==0) && (strcmp(date('d',$start_date),date('d',$end_date))!==0)){
$newDate = date('Y', ($start_date));
echo date('d',$start_date).' '.date('F',$start_date).'-'.date('d',$end_date).' '.date('F',$end_date).' '.$newDate;
}
elseif((strcmp(date('n',$start_date),date('n',$end_date))!==0) && (strcmp(date('Y',$start_date),date('Y',$end_date))!==0) && (strcmp(date('d',$start_date),date('d',$end_date))!==0)){
echo date('d',$start_date).' '.date('F',$start_date).' '.date('Y',$start_date).'-'.date('d',$end_date).' '.date('F',$end_date).' '.date('Y',$end_date);
}
else{
echo 'Do same as for different case';
}
?>
Also refer to this solved solution