- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
PHP program to calculate the total time given an array of times
The ‘strtotime’ function can be used to convert the given string into a time format. Let us see an example −
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"); ?>
Output
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.
- Related Articles
- Java Program to calculate the time of sorting an array
- PHP program to sort dates given in the form of an array
- Program to calculate Bitonicity of an Array
- Calculate total time duration (add time) in MySQL?
- PHP program to find the total number of date between any two given dates
- C++ Program to calculate Bitonicity of an Array
- How to calculate total time between a list of entries?
- Swift Program to Calculate the sum of Elements in a Given Array
- PHP program to split a given comma delimited string into an array of values
- PHP program to convert a given timestamp into time ago
- Java Program to Calculate the Execution Time of Methods
- PHP program to compute the execution time of a PHP script
- 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 find the minimum element in an array

Advertisements