I have a question about how to convert seconds to hour and minute and second with no PHP etc. But we can just use + and - and / and * in our calculation. I was wondering if you could answer me sooner I searched it a lot but I couldn’t find it in Google.
The example is this :
6:50:30 + 3:55:20 = ??:??:??
I can calculate it but I need the formula. Thanks
It’s probably easiest to show you how it’s done in PHP, and then if you want to, you can use the same formula outside of PHP as requested.
I’ll provide a code sample, but here’s a quick overview.
- Convert each of the times into a timestamp (i.e., seconds), using a service like this one: https://www.unixtimestamp.com/
A Unix timestamp (or epoch time) is the number of seconds that have elapsed since January 1, 1970 00:00 UTC.
- Next, add or subtract to find the desired calculation.
Quick Example in PHP, using strtotime()
<?php $time1 = strtotime('2017-01-01 06:50:30'); // YYYY-MM-DD HH:MM:SS $time2 = strtotime('2017-01-01 15:55:20'); $difference = abs($time1 - $time2); // Difference, in seconds. $in_hours = $difference / 3600; // Divides by seconds in 1 hour. ?>
Tip: It’s good to be aware of the timezone also, particularly if you decide to use a programming language like PHP to perform the calculation. That said, if you’re just doing this manually, then use a service like this one to get the timestamps before you do the math: https://www.unixtimestamp.com/