每天循环播放数小时

时间:2015-03-27 12:18:21

标签: php loops hour

我真的陷入了代码,我不知道如何解决它。我想计算一下我赚多少钱。问题是我根据一天中的哪个小时赚取不同的金额。 Fx的:

08.00 - 06.00  (Morning) -> 150
06.00 - 10.00 (Evening) -> 200
10.00 - 11.00 (Night) -> 250

所以我用这段代码计算了一天工作的小时数:

<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> 

让我说我工作时间从早上10点到晚上11点。那意味着我工作了13个小时。但是我如何计算在上述时间段内我赚多少钱?我尝试了以下但没有任何工作:

<?php


// create timeblocks:
// 8  = from 0.00 untill 8.00 
// 18 = from 8.00 untill 18.00
// 24 = from 18.00 untill 0.00
$timeblocks=array(
    8*60,
    18*60,
    24*60
    );
//earnings PER 15 MINUTES in the above timeblocks
$money=array(
    146/4,
    204/4,
    244/4
    );
$earned=0;

//create a start and end time object
$start=date_create_from_format('Y-m-d H:i:s', $_POST['s1']);
$end=date_create_from_format('Y-m-d H:i:s', $_POST['s2']);

//calculate how many blocks of 15 minutes
$diff=$start->diff($end);
$steps=(($diff->d)*3600+($diff->h)*60+($diff->i))/15;

//loop every 15 minutes
for($i=0;$i<=$steps;$i++){
    //start with the start date, and add 15 minutes every time
    if($i!==0)$start->add(new DateInterval('PT15M'));

    //calculate the time 
    $time=((int)($start->format('H')*60)+(int)($start->format('i')));

    //look in timeblocks what block we are in and get the earnings from that block
    for($block=0;$block<3;$block++){
        if($time<=$timeblocks[$block]){
            $earned+=$money[$block];
            echo '$'.$money[$block].' earned at '.$start->format('d-m-Y H:i:s').'<br>';
            break;
            }
        }
    }

echo 'TOTAL earned: $'.$earned; 


?>

我希望有人可以帮助我。

最诚挚的问候 MADS

3 个答案:

答案 0 :(得分:0)

$begin = new DateTime( 'YYYY-MM-DD h:I' );
$end = new DateTime( 'YYYY-MM-DD h:I' );


$interval = new DateInterval('P1I');
$daterange = new DatePeriod($begin, $interval ,$end);

foreach($daterange as $date){
    //each of the hours in between.
    //do the checks on the hour here $date will be the variable your interested in
}

我建议使用日期时间,当DST等进入游戏时,小时变得越来越复杂

答案 1 :(得分:0)

更改您的HTML代码会更容易取代您的&#34; dot-minutes&#34;每个value标记的option属性中包含.25,.5和.75的值。例如:

 <option value="13.75">13:45</option>

然后,以下PHP代码将执行您期望的操作

您的原始代码:

<?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_salary = 150;
    $middle_time_salary = 200;
    $late_time_salary = 250;

要添加的代码

 $hour0=$end_time-$start_time;
 if ($hour0>0) {
   $hour1=($end_time>18) ? $end_time-18 : 0;
   $hour2=($end_time>22) ? $end_time-22 : 0;

   $tot=($hour0*150) + $hour1*50 + $hour2*50;

   echo "Total earned: $tot";
 } else {
   echo "Error : work finished before it starts";
 }
}

答案 2 :(得分:0)

我确定有更好的解决方案,但这样可行:-)

[edit] 以满足OP的新要求

//If you haven't done allready SET your timezone!!
date_default_timezone_set('Europe/Amsterdam');

// create timeblocks:
// 8  = from 0.00 untill 8.00 
// 18 = from 8.00 untill 18.00
// 24 = from 18.00 untill 0.00
$timeblocks=array(
    8*60,
    18*60,
    24*60
    );
//earnings PER 15 MINUTES in the above timeblocks
$money=array(
    100/4,
    10/4,
    50/4
    );
$earned=0;

$_POST['s1']='18.30';
$_POST['s2']='8.30';

//sanitize your post: only numbers and a dot allowed
$s1=preg_replace("/[^0-9|.]/","",$_POST['s1']);
$s2=preg_replace("/[^0-9|.]/","",$_POST['s2']);

if($s1==$s2){
    //no working time: do something
    //NOTE: this assumes nobody working for 24 hours, because if you start at 
    //6pm day 1 and end 6pm on the next day (==24 hours) the times are the same 
    //as well.
    }

//if second time smaller then first time, we are in the next day and add 24 hours
$add=0;
if($s1>$s2)$add=24;

//make hours and minutes
$s1=explode('.',substr('0'.$s1,-5));
$s2=explode('.',substr('0'.$s2,-5));

//create a start and end time object
$start=new Datetime('now');
$start->setTime($s1[0],$s1[1]);

$end=new Datetime('now');
$end->setTime($s2[0]+$add,$s2[1]);


//calculate how many blocks of 15 minutes
$diff=$start->diff($end);
$steps=(($diff->d)*3600+($diff->h)*60+($diff->i))/15;

//loop every 15 minutes
for($i=1;$i<=$steps;$i++){
    //start with the start date, and add 15 minutes every time
    $start->add(new DateInterval('PT15M'));

    //calculate the time 
    $time=((int)($start->format('H')*60)+(int)($start->format('i')));

    //look in timeblocks what block we are in and get the earnings from that block
    for($block=0;$block<3;$block++){
        if($time<=$timeblocks[$block]){
            $earned+=$money[$block];
            echo '$'.$money[$block].' earned at '.$start->format('d-m-Y H:i:s').'<br>';
            break;
            }
        }
    }

echo 'TOTAL earned: $'.$earned;