计算两个数据时间之间给定时间段内的小时数 [英] Calculate the number of hours in a given timeframe between two datetimes

查看:237
本文介绍了计算两个数据时间之间给定时间段内的小时数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预订系统,用户可以随时预订房间,连续几天。预订将根据房间使用的分钟数量收费。

I have a booking system where users can book a room at any time, and for any number of continuous days. The booking is charged depending on the number of minutes the room is in use.

在预订系统中,开始和结束时间由两个时间戳表示。例如

In the booking system, the start and end time is represented by two timestamps. e.g.

start_time = 1397124000
end_time = 1397129400

从这里我可以计算预订的秒数,因此计算费用。

From this I can calculate the number of seconds booked, and therefore calculate a charge.

我遇到的问题是,我想计算在高峰时段 - 上午8点之前和晚上6点之间的任何预订的50%的折扣。这些时间可以预订(即上午7点至上午9点),所以适当的比例应该被打折(7-9am预订的7-8am部分的50%折扣)。

The problem I have is that I'd like to calculate a 50% discount on any bookings made out of peak times - before 8am, and after 6pm. Bookings can be made across these times (i.e. 7am-9am), and so the appropriate proportion should be discounted (50% discount on the 7-8am portion of the 7-9am booking).

我的代码用于计算这个是非常困惑和复杂的,因为有几种情况:

My code for calculating this is getting extremely confusing and complicated, as there are several scenarios:


  1. 预订在折扣期间开始和结束(例如上午3点至7点 - 4小时折扣)

  2. 预订在折扣期间开始,但之后结束(例如上午7点至上午9点 - 1小时优惠)

  3. 预订在非优惠期间开始和结束(上午10点至下午5点 - 不折扣)

  4. 预订开始在非折扣期间,但之后结束(下午5点至10点 - 1小时折扣)

  5. 预订跨越整个预打折扣期间(上午2点至10点 - 10小时)打折)或更复杂(第5天下午1点至晚上10点 - 折扣50小时)。

  1. The booking starts and ends during a discount period (e.g. 3am-7am - 4 hours discounted)
  2. The booking starts during a discount, but ends afterwards (e.g. 7am-9am - 1 hour discounted)
  3. The booking starts and ends during a period of non-discount (10am-5pm - no discount)
  4. The booking starts during a period of non-discount, but ends afterwards (5pm-10pm - 1 hour discounted)
  5. The booking spans an entire before-during-after discount period (2am-10pm - 10 hours discounted) or even more complicated (2am on Day 1 to 10pm on Day 5 - 50 hours discounted).

在上述8am之前的6pm的折扣期间,只提供开始和结束的时间戳是非常困难的。

At the moment trying to work out what proportion of a booking is during these pre-8am, post-6pm discount period when only provided with a start and end timestamp is very difficult.

我的代码是一个非常大的系列,如果 else 对开始和结束时间进行测试的语句,但是对于超过一天的预订而言并不强大,否则会非常混乱。

My code is a very large series of if and else statements testing against start and end times, but it is not robust against bookings that span more than one day and is otherwise very messy.

我正在寻找一种方法,可以轻松计算折扣时间内预计的比例,这是所有可能的情况。一个困难的问题!

I'm looking for a method that can easily calculate what proportion of a booking is within a discounted time, that accounts for all possible scenarios. A difficult problem!

推荐答案

在考虑了很多选项后,最终发生了以下解决方案。

After pondering many options, the following solution eventually occurred to me.

我写了一个功能,以30分钟的间隔移动预订开始和结束时间,检查每次是否在一个折扣期限内:

I wrote a function to move through the booking start and end times in 30 minute intervals, check to see if each time was within a discounted period:

function discount_period($timestamp) {
    $lowerTime = mktime (8,0,0,date("n", $timestamp), date("j", $timestamp), date("Y", $timestamp));
    $upperTime = mktime (18,0,0,date("n", $timestamp), date("j", $timestamp), date("Y", $timestamp));
    if (($timestamp < $lowerTime) || ($timestamp >= $upperTime)) {
            return true;
    }
    else {
            return false;   
    }
}

$discountmins = 0;
$nondiscountmins = 0;

for ($i = strtotime($billingResult->start_time); $i < strtotime($billingResult->end_time); $i += 1800) {
    if (discount_period($i)) {
        // This is within a discount period of at least 30 minutes
        $discountmins += 30;
    }
    else {
        $nondiscountmins +=30;  
    }
}
$discountedcost = ((($discountmins / 60) * $billingResult->cost_per_hr) * 0.5) / 100;
$nondiscountcost = (($nondiscountmins / 60) * $billingResult->cost_per_hr) / 100;

所以它基本上检查下一个30分钟的窗口是否在折扣期内 - 如果是,它会增加打折的分钟计数器,如果没有打印的分钟计数器。最后,它们只是根据分钟数计算成本。

So it essentially checks to see if the next 30 minute window is within a discount period - if it is, it increments the discounted minute counter, or the non-discounted minute counter if not. At the end it them merely calculates the costs based on the number of minutes.

这篇关于计算两个数据时间之间给定时间段内的小时数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆