leonardeveloper
leonardeveloper

Reputation: 1853

How to calculate seconds in php

I have this code:

    $hours = floor($differenceInHours);
    $minutes = ($differenceInHours-$hours)*60;
    $seconds = ':00';
    $total=$hours . ":" . $minutes .':' . $seconds;
    echo $total;

I want to know on how to calculate the seconds. Any formula?

Upvotes: 0

Views: 708

Answers (1)

Tomas Creemers
Tomas Creemers

Reputation: 2715

You can use the second parameter of the date function to format a number of seconds into a formatted time notation. As soon as the number of hours is larger than 24, it considers that to be 1 day, so you'll have to take that into account by treating the hours separately.

echo floor($differenceInHours) . ':' . date('i:s', ($differenceInHours - floor($differenceInHours)) * 3600);

If you do not want to treat the hours separately, you can use:

echo date('d H:i:s', $differenceInHours * 3600);

(Of course, when it reaches 31 days, it considers it a month, et cetera...)

Upvotes: 2

Related Questions