get average hours in php language

php time difference in hours
php dateinterval
how to calculate total working hours in php
php calculate time difference between two dates in hours and minutes
php datetime
php calculate time difference between two dates in minutes
php dateinterval::format
php date_diff

I have a table with 3 columns: id start_service stop_service

I have already managed to catch the time difference between start_service and stop_service using this query:

$query = "SELECT
  TIMEDIFF (stop_service, start_service)  AS tempo 
  FROM
  user_establishment ORDER BY id";

Now I need to add all the results of this query and divide by the number of records, so as to obtain the average time of all services.

The main problem is the conversion of the hours.

Can someone please help me?

Following query will be used to get average of time difference. No need to add ORDER BY clause while using AVG() function in mysql query. It saves time to being execute.

$query = "SELECT AVG(TIMEDIFF(stop_service, start_service)) AS tempo FROM user_establishment";

How to calculate total time of an array in PHP ?, Given an array containing the time in hr:min:sec format. The task is to calculate the total time. If the total time is greater then 24 hours then the� PHP is a world-wide used server-side scripting programming language that is used to develop dynamic and static websites. Get details of PHP Language, Course, Admission, Fees, Institutes, Duration, Career And Job.

Try this if you need to get your results in hours. You can omit the division by 3600 if you need to have it in seconds (this is what I would do and then manipulate it in my code afterwards).

SELECT
AVG(TIME_TO_SEC(TIMEDIFF(stop_service, start_service)))/3600  AS tempo 
FROM
user_establishment;

Hope this helps!

date_diff - Manual, Powerful Function to get two date difference. '%y Year %m Month %d Day %h Hours %i Minute %s Seconds' => 1 Year 3 Month 14 Day 11 Hours 49 Minute� Learn the PHP programming language in this full course / tutorial. The course is designed for new programmers, and will introduce common programming topics u

If you want the average time difference in hours upto two decimal points, you can try the below query.

select ROUND(AVG(TIME_TO_SEC(tempo)/3600), 2) as avg_time
from (
    select TIMEDIFF(stop_service, start_service) as tempo
    from user_establishment
) as timeline

You can modify the average time diff as per your need.

DateInterval::format - Manual, Change language: H, Hours, numeric, at least 2 digits with leading 0, 01, 03, 23 It is not possible to calculate how many days are in a given year or month� Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

Use this query for the average

$query ="SELECT TIME_FORMAT(SEC_TO_TIME(AVG(TIME_TO_SEC(
         TIMEDIFF( stop_service , start_service)))),"%H %i")  AS tempo 
         FROM
         user_establishment";
user $conn = $conexao->query($consulta) or die($conexao->error); 
while ($resultado = $conn->fetch_array()) 
  { echo $resultado["tempo"];  
  }
create table user_establishment ( stop_service datetime, start_service dateTIME)
insert into user_establishment ( stop_service , start_service) Values 
("2020-01-10 13:10:11", "2020-01-10 11:10:10"),
("2020-01-10 13:10:11", "2020-01-10 11:10:10"),
("2020-01-10 13:10:11", "2020-01-10 11:10:10"),
("2020-01-10 13:10:11", "2020-01-10 11:10:10");
SELECT TIME_FORMAT(SEC_TO_TIME(AVG(TIME_TO_SEC(
       TIMEDIFF( stop_service , start_service)))),"%H:%i") as tempo
FROM user_establishment;
| tempo |
| :---- |
| 02:00 |

db<>fiddle here

time - Manual, Change language: Local time as string can be get by strftime() and local timestamp (if ever time_elapsed_B: 6 days 15 hours 48 minutes and 19 seconds ago. -OR- days and hrs -OR- days depending on how many seconds are given. PHP's time() function gives you all the information that you need about the current date and time. It requires no arguments but returns an integer. It requires no arguments but returns an integer. The integer returned by time() represents the number of seconds elapsed since midnight GMT on January 1, 1970.

DateInterval - Manual, Change language: English, Brazilian to "1 month and 1 day". If you still want to calculate the seconds into hours / days / years, etc do the following: <?php The output from date() will show the local time of the server. You may need to perform timezone adjustments, or else override the server's time zone as mentioned in other posts.

cal_days_in_month - Manual, Change language: English, Brazilian Here's a one-line function I just wrote to find the numbers of days in a month that doesn't depend on any other functions. A better way to get a nice time-format (1 year ago, 2 months until) without all the trailing months, days, hours, minutes, seconds in the result is by using the DateTime format and using the date_diff function as they both does most of the heavy lifting for you Function below as example <?php // Get time interval function

array_sum - Manual, Change language: array_sum — Calculate the sum of values in an array If you want to find the AVERAGE of the values in your array, use the sum and count � PHP Version: 4+ Changelog: PHP 5.1.0: Added E_STRICT and E_NOTICE time zone errors. Valid range of timestamp is now from Fri, 13 Dec 1901 20:45:54 GMT to Tue, 19 Jan 2038 03:14:07 GMT. Before version 5.1.0 timestamp was limited from 01-01-1970 to 19-01-2038 on some systems (e.g. Windows).

Comments
  • The post needs to be in English.
  • @FunkFortyNiner Sorry, i fixed
  • Thank you Eduardo. Have a look at an answer given below.
  • in the "service start" field I have the following time: 03:19:15 in the field "fim_servico" I have the following time: 03:20:18 the result I got with your code was: 54.0000 it would have to be in hours
  • It is getting the average result of all the rows at once. I think you are looking to calculate the difference between stop & start in hours record wise? am i correct?
  • I need to calculate the average of the difference between start_service and end_service and show the result in hours format too
  • FROM (SELECT TIMEDIFF (inicio_servico, fim_servico) AS tempo FROM usuario_estabelecimento) tempo"; $conn = $conexao->query($consulta) or die($conexao->error); while ($resultado = $conn->fetch_array()) { echo $resultado["tempo"]; } I HAVE THE ERROR: UNDEFINED INDEX 'TEMPO'
  • I need to calculate the average of the difference between start_service and end_service and show the result em hours and minutes