每 15 分钟在两个时间点之间循环计算总收入

Calculate total earnings while looping between two points of time every 15 minutes

我真的卡在代码里了,不知道怎么解决。我正在计算我一天赚多少钱。问题是我赚取的金额取决于一天中的哪个小时。外汇:

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; 


?>

我希望有人能帮助我。

最好的问候 疯狂

$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 等进入午夜时,时间会变得更复杂

在每个 optionvalue 属性中,更改 HTML 代码将您的 "dot-minutes" 值替换为 .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";
 }
}

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

[编辑]以满足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; 

我会这样处理:

  1. 为每个 time-based 工资变化创建一个包含元素的查找数组。
  2. 创建一个 DateTime Period 以在传入的两个时间之间每 15 分钟迭代一次。
  3. 每 15 分钟重复一次,计算正确的工资并将该金额添加到您的总数中。

代码:(Demo)

$_POST['s1'] = '16.15';
$_POST['s2'] = '23.30';

$wagesLookup = [
    '18:00' => 146 / 4,  // morning
    '22:00' => 204 / 4,  // evening
    '24:00' => 244 / 4   // night
];

$period = new DatePeriod(
     DateTime::createFromFormat('H.i', $_POST['s1']),
     new DateInterval('PT15M'),
     DateTime::createFromFormat('H.i:01', "{$_POST['s2']}:01")  // bump ahead to make "inclusive"
);

$total = 0;
foreach ($period as $dt) {
    $time = $dt->format('H:i');
    foreach ($wagesLookup as $mark => $pay) {
        if ($time <= $mark) {
            break;
        }
    }
    printf("%s earned %.02f\n", $dt->format('H:i'), $pay);
    $total += $pay;
}
echo "Total: $total";

输出:

16:15 earned 36.50
16:30 earned 36.50
16:45 earned 36.50
17:00 earned 36.50
17:15 earned 36.50
17:30 earned 36.50
17:45 earned 36.50
18:00 earned 36.50
18:15 earned 51.00
18:30 earned 51.00
18:45 earned 51.00
19:00 earned 51.00
19:15 earned 51.00
19:30 earned 51.00
19:45 earned 51.00
20:00 earned 51.00
20:15 earned 51.00
20:30 earned 51.00
20:45 earned 51.00
21:00 earned 51.00
21:15 earned 51.00
21:30 earned 51.00
21:45 earned 51.00
22:00 earned 51.00
22:15 earned 61.00
22:30 earned 61.00
22:45 earned 61.00
23:00 earned 61.00
23:15 earned 61.00
Total: 1413