Calculate total days worked in a certain month

I am having a problem creating a system in PHP / MySQL. I have a form I need to insert into:

  • name of Employee
  • date of entry on Duty
  • exit date

Usually happens something like in the example: the employee enters the day 29/04/2014 and leaves the day 03/05/2014. I need to set 2 days worked for April, and 3 days worked for May. How can I solve this problem? And I do not know what the best way to create the tables in the bank. Could someone give a light?

 2
Author: bfavaretto, 2014-05-14

2 answers

1) convert dates to PHP timestamp. Ex:

$entrada = mktime(0,0,0,4,29,2014); //29/04/2014
$saida = mktime(0,0,0,5,3,2014); // 03/05/2014

2) evaluate if there is a difference in months. If there is a difference you should make two calculations, one for each month. If there is no you just need to subtract the dates.

$mesEntrada = date('n', $entrada);
$mesSaida = date('n', $saida );

if ($mesEntrada != $mesSaida)
{
    $diasTrabalhadosMesEntrada = (date("t", $entrada)-date("j",$entrada))+1; // Subtrai o total de dias do mes pelo dia de entrada.
    $diasTrabalhadosMesSaida = date('j', $saida ); // A quantidade de dias trabalhados é igual ao dia de saída.

    // Inserir no BD...
}
else
{
   // Apenas 1 mes (mesEntrada igual a mesSaida);
   $diasTrabalhados = date("j",$saida)-date("j",$entrada);

   // Inserir no BD...
}

With respect to the database, you can create a table that stores in separate columns the month, the year and the number of days worked (columns funcionario, month, year and days_worked). I advise you to also create a history table that stores all entries and employee outputs (employee columns, input and output).

I suggest consulting http://www.php.net/manual/pt_BR/function.date.php for a better understanding.

I haven't tested the code, it's just a targeting.

 2
Author: Arivan Bastos, 2014-05-16 02:50:18

How about this:

Staff Table:

  • id
  • name

Table shifts:

  • id
  • official_id
  • entry
  • saida

And then you get the days worked doing saida - entrada. If this information is not to be changed later, you can even create a column for it in thebella plantões.

 1
Author: , 2014-05-15 11:14:29