How to create a datetime from a custom format

With built-in php formatting facilities, you can create a datetime from however crazy sign sequence.

First, consider pretty standard ISO8601 string: 2018-12-31T23:12:59+0200. It has an Y-m-d\TH:i:sO ISO8601 format. So, the following code results into almost identical datetime string (mind last colon sign in timezone offset):

(new FromCustomFormat(
    'Y-m-d\TH:i:sO',
    '2018-12-31T23:12:59+0200'
))
    ->value(); // returns a '2018-12-31T23:12:59+02:00' string

Now consider a more esoteric sequence, say, 122018--31TT!23:12:59+0200. If you take a close look, it has a mY--d\T\T\!H:i:sO format. Indeed, the following code returns 2018-12-31T23:12:59+02:00:

(new FromCustomFormat(
    'mY--d\T\T\!H:i:sO',
    '122018--31TT!23:12:59+0200'
))
    ->value();

After you’ve obtained an ISO8601DateTime object, you can do lots of stuff: add seconds, minutes, hours, days, months, years to it, calculate a difference between datetimes, convert it into any other timezone, and much more. Consider quick start entry for more info.