Calculation in % between PHP time loads

Asked

Viewed 65 times

1

I need to calculate the achieved adhesion between two hours.

  • Charge 1: Charge is the end time - the start time
  • Charge 2: Scheduled load is the scheduled end time - the scheduled start time.

at the precise end of the percentage 0.95 por exemplo Load 1 / Load 2 in excel this works in Javascript too but in PHP I’m having problems.

I printed the data:

Realizado: 2016-10-24 15:10:00 - 2016-10-24 07:39:12 = Carga1: 07:30:48
Programado: 15:10:00 - 07:00:00 = Carga2: 08:10:00
Percentual: 0.875

*The accomplished format comes with date and the programmed only time, I tried to leave the two in the same format but it did not work.

I’m going this way:

$date1 = strtotime($ponto_inicio);
$date2 = strtotime($ponto_fim);
$realizado = date( 'H:i:s', abs( $date2 - $date1 ) );

$date3 = strtotime($hora_inicio);
$date4 = strtotime($hora_fim);
$programado = date( 'H:i:s', abs( $date4 - $date3 ) );

$percentual =  $realizado / $programado;

100% results are ok but when it comes out of it the result is always 0.88 for different scheduled time.

What is wrong?

  • You can find examples of $starting point, $ending point, $starting time, and $ending time that are being used in the example?

  • I put the data used.

2 answers

1

Hello! Try:

$percentual = ($realizado * 100)/$programado;
  • It did not work, in this case all results are equal.

0

I got the result by converting the charges to seconds, using the date_paser();

$date1 = strtotime($ponto_inicio);
$date2 = strtotime($ponto_fim);
$realizado = date( 'H:i:s', abs( $date2 - $date1 ) );
$realizado = date_parse($realizado);
$realizado = $realizado['hour'] * 3600 + $realizado['minute'] * 60 + $realizado['second'];

$date3 = strtotime($hora_inicio);
$date4 = strtotime($hora_fim);
$programado = date( 'H:i:s', abs( $date4 - $date3 ) );
$programado = date_parse($programado);
$programado = $programado['hour'] * 3600 + $programado['minute'] * 60 + $programado['second'];

$percentual =  $realizado / $programado;

This way the percentage came out right, I do not know if the structure is totally correct.

  • then it’s not about percentage calculation...

  • Result *100 + "%" is the percentage yes, only it is not formatted, I did not understand what I meant, by the way in the bank later if AVG of the result if you have "%" is not possible, this way is more practical.

Browser other questions tagged

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