If I do this:
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2020-10-13');
$interval = date_diff($datetime1, $datetime2);
var_dump($interval);
It outputs:
["y"]=>
int(11)
["m"]=>
int(0)
["d"]=>
int(2)
["h"]=>
int(0)
["i"]=>
int(0)
["s"]=>
int(0)
["f"]=>
float(0)
I could of course write a routine here to generate a message such as:
11 years and 2 days
by going through each of those variables and constructing a string in English.
However, this would be a lot of thinking and coding (it's more complex than it first looks, especially since "weeks" are lacking in the output), plus it's only in English. I want it to output the same in other languages, and given by how the DateTime class has all kinds of language support, it's more than likely that this exists!
I want to input two timestamps and get back a dynamic string such as these, depending on the available units and the set locale:
35 years, 4 months, 2 weeks, 4 days, 8 hours and 32 minutes
12 år, 1 månad och 42 sekunder
I keep finding golden nuggets hidden deep in PHP, and sadly, reading the manual rarely allows me to stumble upon these things as they are usually "non-obvious" from looking at the examples and user comments. I believe that this might very well exist, since a lot of similar things related to dates/time do exist with full language support.