Calculate total days worked in a given month

Asked

Viewed 793 times

2

I have a problem creating a system in PHP/Mysql. I have a form in which I need to insert:

  • Name of Official
  • Date signed on duty
  • Date of Departure

Usually it happens something like in the example: the employee enters in the day 29/04/2014 and leaves 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 don’t know the best way to create the tables in the bank. Someone could shed some light?

  • Hello, try to do something like this: $dia2 = date('d')+2;
if($dia2 !== date('d')){
 //Hoje nao é o dia de saída insere a data actual
 //insert into x where xxx values date('d/M/Y');
 } else {
 //insert into x where xxx values date
 }

2 answers

2


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 the months. If there is a difference you should make two calculations, one for each month. If there is not 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...
}

Regarding the BD, you can create a table that stores in different columns the month, year and number of days worked (columns employee, month, year and days worked). I also recommend creating a history table that stores all employee inputs and outputs (function columns, input and output).

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

I didn’t test the code, it’s just a direction.

  • Why convert them to timestamp?

  • To facilitate date operations.

  • @Arivanbastos the $daysWorkingMesEntry is not working, it only returns me 0, even when the employee works. = / //no wonder I respond with two profiles, is that in my work can not login with facebook.

  • @Joãoneto, there was an error in the code. I fixed it. But as I said it is just a direction, seek to debug and understand the logic, so you learn +! :-)

  • Thanks Arivan, I had decided that I would do this, and I would not ask for help here. But I’m making system for a judge, so I’m in a hurry kkkkk. Thanks.

1

How about this:

Staff table:

  • id
  • name

Table shifts:

  • id
  • office_id
  • entree
  • exit

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

  • Hmmm, that’s exactly it. But there is another problem, I need to be recorded, that the employee worked x days in the month y. That way would only show the total right? How would you add the months? So I could display by month?

  • @Joãoneto I think in that case you would need a table dias_trabalhadosor histórico_plantão. Records would be entered into it via application as soon as the user submitted the form. Via PHP you would have to do this algorithm. For example, if it goes 05/05 and leaves 10/05 will only enter a record for the month 05 with 5 days. But if enter 25/05 and exit 05/06 there are inserted 2 records, one for the month 05 (6 days) and another for the 06 (5 days). Then you can add up the shifts that month.

  • @Joãoneto You can do this in a separate table plantões and can resolve to do just this. It depends on your need.

  • That’s the way I thought, I’ll do it. As soon as I get to work I’ll start doing it. I think this solution will suit me well. Thanks for the support there!

  • @Joãoneto I’m happy to help. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.