jeudi 26 mars 2015

loop through working hours

I am trying to figure out to calculate how much I earn throughout a day at work. I have a script now, that calculate how many hours I work a day. But the problem is that my salary is different throughout the day. The point is that I have a starttime and endtime. These hours is saved in hours_worked. So lets say I am working from 03.00 PM to 11.00 PM. Then I need to run through those hours I have choosen, and then give me the salary a day. Fx:



03.00 PM - 11.00 PM:

08.00 - 06.00 PM - 100 kr pr hour -> 300 kr (Morning)
06.00 - 10.00 PM - 150 kr pr hour -> 600 kr (Evening)
10.00 - 11.00 PM - 200 kr pr hour -> 200 kr (Late Evening)


The final result should be 1100 kr.


I have tried to make a while loop, but that didn't do it for me. I tried to make 3 variables, to save the numbers in throughout the loop, and then get them calculated at the end. But I am really in doubt if I have to use while loop, foreach, for loop etc. I would guess I have to get som "if/else" in the code aswell? But can somebody give me a hint what to do?


Best Regards Mads



<?php

if (isset($_POST['submit'])) {

echo "Started time : " . $_POST['s1'] . "<br/>";
echo "End time : " . $_POST['s2'] . "<br/>" ;

$start_time = $_POST['s1'];
$end_time = $_POST['s2'];

$early_time;
$middle_time;
$late_time;

$hours_worked = $end_time - $start_time;

echo "Total hours worked : " . $hours_worked."<br>";
}

?>


HTML:



<body>
<p>Choose Your Work TIme</p>
<form method='post'>
<select name="s1" required>
<option>Choose Start Time</option>
<option value="8.45">8.45</option>
<option value="9.00">9.00</option>
<option value="9.15">9.15</option>
<option value="9.30">9.30</option>
<option value="9.45">9.45</option>
<option value="10.00">10.00</option>
<option value="10.15">10.15</option>
<option value="10.30">10.30</option>
<option value="10.45">10.45</option>
<option value="11.00">11.00</option>
<option value="11.15">11.15</option>
<option value="11.30">11.30</option>
<option value="11.45">11.45</option>
<option value="12.00">12.00</option>
<option value="12.15">12.15</option>
<option value="12.30">12.30</option>
<option value="12.45">12.45</option>
<option value="13.00">13.00</option>
<option value="13.15">13.15</option>
<option value="13.30">13.30</option>
<option value="13.45">13.45</option>
<option value="14.00">14.00</option>
<option value="14.15">14.15</option>
<option value="14.30">14.30</option>
<option value="14.45">14.45</option>
<option value="15.00">15.00</option>
<option value="15.15">15.15</option>
<option value="15.30">15.30</option>
<option value="15.45">15.45</option>
<option value="16.00">16.00</option>
<option value="16.15">16.15</option>
<option value="16.30">16.30</option>
<option value="16.45">16.45</option>
<option value="17.00">17.00</option>
<option value="17.15">17.15</option>
<option value="17.30">17.30</option>
<option value="17.45">17.45</option>
<option value="18.00">18.00</option>
<option value="18.15">18.15</option>
<option value="18.30">18.30</option>
<option value="18.45">18.45</option>
<option value="19.00">19.00</option>
<option value="19.15">19.15</option>
<option value="19.30">19.30</option>
<option value="19.45">19.45</option>
<option value="20.00">20.00</option>
<option value="20.15">20.15</option>
<option value="20.30">20.30</option>
<option value="20.45">20.45</option>
<option value="21.00">21.00</option>
<option value="21.15">21.15</option>
<option value="21.30">21.30</option>
<option value="21.45">21.45</option>
<option value="22.00">22.00</option>
<option value="22.15">22.15</option>
<option value="22.30">22.30</option>
<option value="22.45">22.45</option>
<option value="23.00">23.00</option>
<option value="23.15">23.15</option>
<option value="23.30">23.30</option>
</select>
<select name="s2" required>
<option>Choose End Time</option>
<option value="8.45">8.45</option>
<option value="9.00">9.00</option>
<option value="9.15">9.15</option>
<option value="9.30">9.30</option>
<option value="9.45">9.45</option>
<option value="10.00">10.00</option>
<option value="10.15">10.15</option>
<option value="10.30">10.30</option>
<option value="10.45">10.45</option>
<option value="11.00">11.00</option>
<option value="11.15">11.15</option>
<option value="11.30">11.30</option>
<option value="11.45">11.45</option>
<option value="12.00">12.00</option>
<option value="12.15">12.15</option>
<option value="12.30">12.30</option>
<option value="12.45">12.45</option>
<option value="13.00">13.00</option>
<option value="13.15">13.15</option>
<option value="13.30">13.30</option>
<option value="13.45">13.45</option>
<option value="14.00">14.00</option>
<option value="14.15">14.15</option>
<option value="14.30">14.30</option>
<option value="14.45">14.45</option>
<option value="15.00">15.00</option>
<option value="15.15">15.15</option>
<option value="15.30">15.30</option>
<option value="15.45">15.45</option>
<option value="16.00">16.00</option>
<option value="16.15">16.15</option>
<option value="16.30">16.30</option>
<option value="16.45">16.45</option>
<option value="17.00">17.00</option>
<option value="17.15">17.15</option>
<option value="17.30">17.30</option>
<option value="17.45">17.45</option>
<option value="18.00">18.00</option>
<option value="18.15">18.15</option>
<option value="18.30">18.30</option>
<option value="18.45">18.45</option>
<option value="19.00">19.00</option>
<option value="19.15">19.15</option>
<option value="19.30">19.30</option>
<option value="19.45">19.45</option>
<option value="20.00">20.00</option>
<option value="20.15">20.15</option>
<option value="20.30">20.30</option>
<option value="20.45">20.45</option>
<option value="21.00">21.00</option>
<option value="21.15">21.15</option>
<option value="21.30">21.30</option>
<option value="21.45">21.45</option>
<option value="22.00">22.00</option>
<option value="22.15">22.15</option>
<option value="22.30">22.30</option>
<option value="22.45">22.45</option>
<option value="23.00">23.00</option>
<option value="23.15">23.15</option>
<option value="23.30">23.30</option>
</select>
<input type="submit" name="submit" Value="Submit">
</form>
</body>

Aucun commentaire:

Enregistrer un commentaire