php – Two date range return

exception or error:

How to return the two date combine string like below.

Start Date : 11/2/2020
End Date : 26/2/2020

Then Output should be like this : 11 to 26 feb 2020

Start Date : 26/2/2020
End Date : 5/3/2020

Then Output should be like this : 26 feb to 5 march 2020

Start Date : 29/12/2020
End Date : 2/1/2021

Then Output should be like this : 29 Dec 2020 to 2 Jan 2021

Please Help me to solve this issue.

How to solve:

Welcome to stackoverflow 🙂

PHP has a native DateTime class which allows you to perform date manipulations and date-formatting.

In your specific case you could start to convert your two dates into objects and then reformat it into your expected format.

Here is an example:

$startDateTime = DateTime::createFromFormat('d/m/Y', $startString);
$startDateTime->format('d M Y');

With this in mind you can then later output the string as desired. I added multiple ways as this is very subjective.

$startFormat = $start->format('d M Y');
$endFormat = $end->format('d M Y');

echo sprintf("%s to %s", $startFormat, $endFormat);

Leave a Reply

Your email address will not be published. Required fields are marked *