PHP and mysql hour count

Good Night,

I'm new here.

A doubt I think is basic.

I will create an" application " with PHP and mysql where I control the working hours of each employee with daily inputs and outputs. The doubt is SQL just stores the data who does the " counting work is PHP in the case ? to work with this type of dates where the goal is to count the weekly and monthly working hours what type of data I use in mysql the type DATE or string and in php do the casting ?

Since now I thank you for your attention

Author: Thiago Ferdinando Rabaioli, 2016-07-31

1 answers

I will divide the questions to better answer, then then solve the problem (or suggest a solution) to the case.

MySQL just stores and who "does the job" is PHP in the case?

Yes and no. MySQL is much more than just a SELECT coluna FROM, you can add, subtract, concatenate (...) and various and various other operations. Many people, myself included, always recommend using MySQL functions in relation to processing them in PHP, when possible.

Example:

Calculate total " earnings "from the table "Financial" where "1 = 1" (that is, everything).

Using MySQL:

$query = mysqli_query($sql, SELECT sum(Ganhos) FROM financeiro WHERE 1 = 1);
list($total) = mysqli_fetch_row($query);

echo $total;

Answer:

100

You don't have to keep looping each value to get the sum of everything, the sum can be done by SUM(). :)

What type of data do I use in MySQL? The type DATE or string and in php do the casting?

It's always better to use that that was assigned to him. Precisely so that you can make use of the native functions of MySQL. In the previous example, if the "gains" field was a VARCHAR it would be "impossible" to use sum(), i.e. it would be impossible to sum the data via MySQL. to use the sum() in this condition would have to use the cast() and could still have errors in cases of some null values, let's agree that choosing the INT would be better than a VARCHAR, in this case of the example.

You can use "VARCHAR for everything" as long as you give up all the resources that MySQL (or any other database) can offer, or need "gambiarras" and more codes to be able to use them. It is logical that some cases there may be more than one field to be chosen, a date may choose DATETIME or TIMESTAMP and may choose INT or BIGINT. Overall, choosing "wrong" can also have overall processing cost and performance.

Solving your problem

Imagine it table:

CREATE TABLE tempo
    (`id` int, `usuario` varchar(32), `dataEntrada` int(11), `dataSaida` int(11))
;

Imagine this data:

INSERT INTO tempo
    (`id`, `usuario`, `dataEntrada`, `dataSaida`)
VALUES
    (1, 'Inkeliz', '1451638800', '1451671200'),
    (2, 'Thiago', '1451638800', '1451667600'),
    (3, 'Inkeliz', '1451721600', '1451764800')
;

The data of dataEntrada and dataSaida are in UNIX TIME, this is measured in seconds since 01/01/1970, unless mistaken. So I'm using INT, because the data will be entered as:

mysqli_query($sql, 'UPDATE dataSaida SET dataSaida = "'.time().'" WHERE id = (SELECT * FROM (SELECT id FROM tempo WHERE usuario = "Thiago" ORDER BY id DESC LIMIT 1) as x)');

This will get the last id where the user is Thiago, then update the exit time. The input time would be set by INSERT earlier.

To see for example, the number of hours worked can use:

$tempoTrabalho = mysqli_query($sql, 'SELECT usuario, TIME_FORMAT(SEC_TO_TIME(SUM(ABS(`dataSaida`- `dataEntrada`))),"%H Horas %i Minutos") FROM tempo WHERE dataSaida != 0 GROUP BY usuario');

foreach(mysqli_fetch_all($tempoTrabalho) as list($usuario, $tempo)){

    echo $usuario.' trabalhou por '.$tempo;
    echo '<br>';

}

Result:

Inkeliz trabalhou por 21 Horas 00 Minutos
Thiago trabalhou por 08 Horas 00 Minutos

This example can be complex for anyone starting out in MySQL / PHP!

The big difference of this code is that all the work is done by MySQL, PHP just has the job of displaying the data, "as is".

Since the function is concentrated in the MySQL query, here is it separate:

SELECT 
usuario, 
TIME_FORMAT(
  SEC_TO_TIME(
    SUM(
      ABS(`dataSaida`- `dataEntrada`)
    )
  ),'%H Horas %i Minutos') 

[...]

GROUP BY 
usuario

See in SQL Fiddle!

Explanations, from within to out:

No SELECT:

ABS = returns the raw value (1 - 100 = 99).

SUM = will sum the result of all (A + B) based on GROUP BY.

SEC_TO_TIME = it will convert the seconds (returned from SUM) to HH:MM:SS.

TIME_FORMAT = it will deliver the data (HH: MM: SS) in the format "0 hours 0 minutes".

In short MySQL is much more than storing data, as suggested. In in case PHP only had the job of displaying the data, no calculation was done by it.

 2
Author: Inkeliz, 2016-07-31 07:00:55