- Related Questions & Answers
- Java Program to calculate the time of sorting an array
- Calculate total time duration (add time) in MySQL?
- How to calculate total time between a list of entries?
- Program to calculate Bitonicity of an Array
- PHP program to sort dates given in the form of an array
- C++ Program to calculate Bitonicity of an Array
- PHP program to find the total number of date between any two given dates
- Write a Golang program to calculate the sum of elements in a given array
- PHP program to calculate the repeated subtraction of two numbers
- PHP program to compute the execution time of a PHP script
- How to calculate the total number of items defined in an enum in C#?
- Java program to calculate mean of given numbers
- PHP program to split a given comma delimited string into an array of values
- Python Program to calculate the Round Trip Time (RTT)
- C Program to calculate the Round Trip Time (RTT)

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The ‘strtotime’ function can be used to convert the given string into a time format. Let us see an example −

<?php $time_arr = [ '00:12:56', '10:11:12', '24:12:44', '50:51:52', '10:10:10' ]; $time = strtotime('00:00:00'); $total_time = 0; foreach( $time_arr as $ele ) { $sec_time = strtotime($ele) - $time; $total_time = $total_time + $sec_time; } $hours = intval($total_time / 3600); $total_time = $total_time - ($hours * 3600); $min = intval($total_time / 60); $sec = $total_time - ($min * 60); print_r("The total time is :"); echo ("$hours:$min:$sec"); ?>

The total time is :-441915:-12:-58

An array that contains time format data is defined and the ‘strtotime´function is used to convert the string into a time format. The ‘foreach’ loop is used to iterate over the elements in the time format array and the elements are added.

The hours is calculated by dividing the value computed by 3600. The minutes is calculated by dividing the value computed by product of hours calculated and 3600. The seconds is calculated by dividing the value computer by product of minutes and 60. The total time computed is displayed on the console.

Advertisements