How can you find out in detail the difference between two dates (today and some, recorded in a variable) so that the result is written in 4 variables: N years, N months, N hours, N minutes? That is, you need exactly 4 separate variables for the subsequent work with them.
Answer 1, authority 100%
Use OOP. DateTime Documentation
$ now = new DateTime (); // current time on the server
$ date = DateTime :: createFromFormat ("Y-m-d H: i", '2014-09-12 23:59'); // set the date in any format
$ interval = $ now- & gt; diff ($ date); // get the difference as a DateInterval object
echo $ interval- & gt; y, "\ n"; // number of years
echo $ interval- & gt; d, "\ n"; // number of days
echo $ interval- & gt; h, "\ n"; // number of hours
echo $ interval- & gt; i, "\ n"; // number of minutes
\ n
added only for the convenience of viewing the results, respectively, instead of echo
we assign to variables or work directly with properties DateInterval
Answer 2, authority 17%
I had to write a function for myself, maybe someone will need it:
function getPeriod ($ date1, $ date2) {
$ interval = date_diff ($ date1, $ date2);
$ y = ''; $ m = ''; $ d = '';
if ($ interval- & gt; y & gt; 0) {
if ($ interval- & gt; y & gt; 4)
$ y. = $ interval- & gt; y. ' years';
else if ($ interval- & gt; y == 1)
$ y. = $ interval- & gt; y. ' year';
else
$ y. = $ interval- & gt; y. ' of the year';
$ y. = ',';
}
if ($ interval- & gt; m & gt; 0) {
if ($ interval- & gt; m & gt; 4)
$ m. = $ interval- & gt; m. 'months';
else if ($ interval- & gt; m & gt; 1)
$ m. = $ interval- & gt; m. 'months';
else
$ m. = $ interval- & gt; m. ' month';
$ m. = ',';
}
if ($ interval- & gt; d & gt; 0) {
if ($ interval- & gt; d & gt; 4)
$ d. = $ interval- & gt; d. 'days';
else if ($ interval- & gt; d & gt; 1)
$ d. = $ interval- & gt; d. 'of the day';
else
$ d. = $ interval- & gt; d. ' day';
}
return $ y. $ m. $ d;
}
…
$ date1 = new DateTime ('2001-01-1');
$ date2 = new DateTime ('2012-12-4');
$ period = getPeriod ($ date1, $ date2);
Sample output:
11 years, 11 months, 1 day
Answer 3
$ date1 = strtotime ([first date]);
$ date2 = strtotime ([second date]);
$ diff = ABS ($ date1 - $ date2);
Next, pass the resulting $ diff
to the formatting function (for example: https://stackoverflow.com/questions/1416697/converting-timestamp-to-time-ago-in-php-eg-1-day-ago -2-days-ago )