seconds to days in php

Example 1

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(1640467);
# 18 days, 23 hours, 41 minutes and 7 seconds

Example 2

public function toDateInterval($seconds) {
    return date_create('@' . (($now = time()) + $seconds))->diff(date_create('@' . $now));
}
echo $this->toDateInterval(1640467)->format('%a days %h hours %i minutes'));
18 days 23 hours 41 minutes


Leave a Reply