0

From Convert seconds into days, hours, minutes and seconds we can get the good function of User Galvic:

function secondsToTime($seconds){
    $dtF = new \DateTime('@0');
    $dtT = new \DateTime("@$seconds");
    return $dtF -> diff($dtT) -> format('%a days, %h hours, %i minutes and %s seconds');
    }

echo secondsToTime(3600 * 24 * 31);

and this work greath!, but I want add months, then I put %m months return 0, and %M months return two digits.

How to convert 3600 * 24 * 32 in [1 month and 2 day] and 3600 * 24 * 30 * 13 in [1 year and 1 month]?

Becosue in Function of Galvic exist many options, but only ONE using 2 lines: GALVIC function, then I want add to this two lines the option "months, year".

Thanks

Stackoverflow
  • 449
  • 5
  • 13

1 Answers1

0

Don't use %a for days, use %d instead.

return $dtF->diff($dtT)->format('%y years, %m months, %d days');
Vladan
  • 1,572
  • 10
  • 23
  • Thanks @Vladan, ```secondsToTime(3600 * 24 * 31);``` return **1 month**; the months in PHP are of 31 days? – Stackoverflow Sep 07 '20 at 17:51
  • @Stackoverflow No, the trick is that we are measuring the difference between January 1st 1970 (@0) and February 1st 1970 (3600 * 24 * 31), so the difference is indeed 1 month. But try setting the start date to February 1st 1970 and compare it against March 2nd for example, you'd get [1 month and 1 day](http://sandbox.onlinephpfunctions.com/code/663473ddc85e668a5aed5ee05a104b4324e0c7dd) – Vladan Sep 07 '20 at 18:11