0

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:

  1. 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).

  2. 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).

  3. 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).

  4. 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.

tgerard
  • 293
  • 3
  • 16
  • Thanks for the replies and apologies for the delay in responding, I was called away unexpectedly. I seem to have put too much emphasis on the format of the dates – I really only wanted to mention it briefly to avoid confusion with the US format. – tgerard Aug 04 '15 at 08:52

2 Answers2

4

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 );
    }
}
doublesharp
  • 26,888
  • 6
  • 52
  • 73
  • Brilliant, I was able to make this work pretty easily (which is saying something, given my rudimentary coding skills). Greatly appreciated. – tgerard Aug 04 '15 at 08:55
-1
<?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

Compare given date with today

Community
  • 1
  • 1
Ujjwal
  • 519
  • 1
  • 5
  • 17
  • This code will not produce the expected results... the `elseif` should not be subtracting the end timestamp from the start, and the formatting generally does not match that of the question. – doublesharp Jul 27 '15 at 07:19
  • Finally found the solution,tested in my local,hope this will help you Mate :) Cheers !! – Ujjwal Jul 27 '15 at 08:33
  • Thanks heaps Ujjwal. This version also worked, but I went for doublesharp's solution because it was a bit more succinct. – tgerard Aug 04 '15 at 08:54