Skip to content
Advertisement

(php) change a duration to something useable by my calendar

I have a series of event dates (variable durations) in the following formats:

  • 28 April 2020
  • 3 – 5 May 2020
  • 3 May – 5 June 2020
  • 20 Dec 2020 – 15 Jan 2021

I want to keep these this way, for readability.

But my calendar needs a standard way of reading these so it can output these events:

  • 28 April 2020 should become 28 April 2020 – 28 April 2020
  • 3 – 5 May 2020 should become 3 May 2020 – 5 May 2020
  • 3 May – 5 June 2020 should become 3 May 2020 – 5 June 2020
  • 20 Dec 2020 – 15 Jan 2021 ==> this the calendar can handle.

Any thoughts on how to do this in php?

Advertisement

Answer

I would really recommend storing your dates as two separate values (start/end) and then formatting on output rather than trying to decode the formatted output.

If you are stuck with this approach, you can use preg_match to match your different formats and then extract the start and end dates from the matched values:

JavaScript

Output:

JavaScript

Demo on 3v4l.org

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement