I have two dates I need to show it in format
29th and 30th November 2024
or
28th to 30th November 2024
or
28th October to 30th November 2024
or
30th November and 1st December2024
What I tried so for is the code below.
$date1 = new DateTime('2024-09-29');
$month1 = $date1->format('m');
$date2 = new DateTime('2024-09-30');
$month2 = $date2->format('m');
$interval = $date1->diff($date2);
if($month1 == $month2){
if($interval->days == 1){
$result_date = getDayWithSuffix($date1);
$result_date .= ' and ';
$result_date .= getDayWithSuffix($date2);
$result_date .= ' '.$date1->format('F');
$result_date .= ' '.$date1->format('Y');
} else {
$result_date = getDayWithSuffix($date1);
$result_date .= ' to ';
$result_date .= getDayWithSuffix($date2);
$result_date .= ' '.$date1->format('F');
$result_date .= ' '.$date1->format('Y');
}
} else {
if($interval->days == 1){
$result_date = getDayWithSuffix($date1);
$result_date .= ' '.$date1->format('F');
$result_date .= ' and ';
$result_date .= getDayWithSuffix($date2);
$result_date .= ' '.$date2->format('F');
$result_date .= ' '.$date1->format('Y');
} else {
$result_date = getDayWithSuffix($date1);
$result_date .= ' '.$date1->format('F');
$result_date .= ' to ';
$result_date .= getDayWithSuffix($date2);
$result_date .= ' '.$date2->format('F');
$result_date .= ' '.$date1->format('Y');
}
}
Is there a better way to achieve this?