Compare timestamps

Hello
Is there an easy way to compare two timestamps in PHP and get the difference in PT (Period of Time)?

For example:
I have an event with a start and endtime.
Blueprint:

              starttime:
                label: Start
                type: time
              endtime:
                label: End
                type: time

To Show the event in a Timetable in the Frontend, I need the PT. For example:
Starttime: 14:00
Endtime: 15:30

<time datetime="14:00PT1H30M">

Another tricky thing is, I have events that surpass midnight, e.g.
Starttime: 23:00
Endtime: 02:00

The Date and Time related extensions in PHP are probably what you need, see PHP: Date/Time - Manual, especially the DateTime and DateInterval classes.

You can use the DateTime class to do all sort of things with dates

https://www.php.net/manual/de/class.datetime.php

There is also a plugin which might be helpful:

It provides ready-to-use field methods

Sounds to me like you are looking for something that gives you the duration… i have a plugin for that…

You may also find this useful (its for repeating events)

And lastly…

Thank you all for the replies! I’m halfway at my goal now with this code:

<?php
$start = $page->starttime();
$end = $page->endtime();
$difference = $start->toDateDiff($end);
?>

<?= $difference->format('%HH') ?><?= $difference->format('%IM') ?>

Any thoughts on how I can get the correct difference with times like 23:00 – 01:00?

what about to convert in a unixtimestamp, and then do the calculations ?

You have multiple options. The most comprehensive one would be to save the date together with the time, then the calculation is straightforward.

If you insist of having only the time and the end time is always later on the same day or on the next day, you just need to check if the difference would be negative (if starttime > endtime) and then either add 24, i.e. (endtime - starttime) + 24 or - if your functions or methods do not like negative values - shift (add) the endtime by the difference of 24 - starttime, which is always positive and then the endtime is your period.