Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫  π  -¹ ² ³ °

You are not logged in.

## #1 2013-07-18 21:17:31

merianos
Member
Registered: 2013-07-18
Posts: 1

### How can I extract integers from an integer series ?

Hello.

I am creating an application, that will allow me to rent a service, based on time. The basic issue is that I apply double charge for pre-defined periods of the day, but this period it is not the same for all of my products.

First I will try to give you the problem in real terms

So, let's say I am renting printers and I have the following two printers:

1) Dot matrix printer
The dot matrix printer it costs \$1 per hour but it costs \$2 per hour between 11:00 and 15:00
2) Laser printer
The laser printer it costs \$4 per hour but it costs \$8 per hour between 20:00 and 01:00 the morning.

Now lets say I have two clients, that are interested for the dot matrix printer. The first client is interested to rent the printer from 08:00 until 09:30 the morning, and the second one is interested to rent the printer from 13:00 until 16:00. Thus, the first client will be charged with \$1.50 because his is renting the printer in single charge period, and the second client must be charged with \$5 because he is renting the printer for two hours in the double charge range period and one hour in single charge period.

Also I have a client that he is interested for the laser printer and he is rent it from 18:00 until the 20:30, so the charge is \$12,00 because he has rent the printer for two hours in the single range period and for a half hour in the double charge period.

Now lets see methimatics

In programming language I am using (PHP), each second has it's own time stamp that it is the total seconds passed from 01/01/1970 until the checked second. So each date/time I check it has it's own timestamp.

Based on the above example.

Lets say today is

07/19/2013

The first client comes to get the printer at 07/19/2013 08:00 and he will return the printer at 07/19/2013 09:30. The timestamps for these dates/times are the following:

Rent start : 1374220800
Rent end  : 1374226200

The double charge period for the selected date starts at 07/19/2013 11:00 and it is end at 07/19/2013 15:00. The timestamps for these dates/times are the following:

Double charge starts : 1374231600
Double charge ends  : 1374246000

Also the current day starts at 07/19/2013 00:00 and it is ends at 07/19/2013 23:59. The timestamps for these dates/times are the following:

Day start : 1374192000
Day ends : 1374278340

Based on the above numbers we have a number range that looks like that:

[Day start : 1374192000] [Rent start : 1374220800] [Rend end : 1374226200] [Double charge start : 1374231600] [Double charge ends : 1374246000] [Day ends : 1374278340]

The second client comes to get the printer at 07/19/2013 13:00 and he will return the printer at 07/19/2013 16:00. The timestamps for these dates/times are the following:

Rent start : 1374238800
Rent end  :  1374249600

The double charge period for the selected date starts at 07/19/2013 11:00 and it is end at 07/19/2013 15:00. The timestamps for these dates/times are the following:

Double charge starts : 1374231600
Double charge ends  : 1374246000

Also the current day starts at 07/19/2013 00:00 and it is ends at 07/19/2013 23:59. The timestamps for these dates/times are the following:

Day start : 1374192000
Day ends : 1374278340

Based on the above numbers we have a number range that looks like that:

[Day start : 1374192000] [Double charge start : 1374231600] [Rent start : 1374238800] [Double charge ends : 1374246000] [Rend end : 1374226200] [Day ends : 1374278340]

Finally, we have the third client that he is interested on Laser printer. So the third client comes to rent the printer at 07/19/2013 18:00 and he will return the printer at 07/19/2013 20:30. The timestamps are the following:

Rent start : 1374256800
Rent end  : 1374265800

In this case the double charge starts today, but stops tomorrow !!. So the double charge period starts at 07/19/2013 20:00 and it is ends at 07/20/2013 01:00. The time stamps for these dates/times are the following:

Double charge starts : 1374264000
Double charge ends  : 1374282000

and the days start / ends are changing acordingly. So the start date is the 07/19/2013 00:00 and the end date is the 07/20/2013 23:59. So the timestamps are the following:

Day start : 1374192000
Day ends : 1374364740

Based on the above numbers we have a number range that looks like that:

[Day start : 1374192000] [Rent start : 1374256800] [Double charge start : 1374264000] [Rend end : 1374265800] [Double charge ends : 1374282000] [Day ends : 1374364740]

The question now

How can I exctract the seconds that involved in the double charge periods ? What is the best formula can be used in order to extract the time duration of the single and double charges ?

Finally, I have create an image the represents this problem in a time period range:

Last edited by merianos (2013-07-18 21:19:06)

Offline

## #2 2013-07-22 06:38:17

SteveB
Member
Registered: 2013-03-07
Posts: 594

### Re: How can I extract integers from an integer series ?

I am not sure exactly which bit you are stuck on but make sure you deal with ALL cases.

In other words something like:

(1) Suppose the customer borrows before the peak rate starts, but returns before the peak rate starts.
(2) Suppose the customer borrows before the peak rate starts, but returns after the peak rate starts, but before it ends.
(3) Suppose the customer borrows before the peak rate starts and returns after the peak rate ends.

(4) Suppose the customer borrows during the peak rate period and returns during the peak rate period.
(5) Suppose the customer borrows during the peak rate period and returns after the end of the peak rate period.

(6) Suppose the customer borrows during the period after the peak rate and ends the loan after the peak rate.

(7) Suppose the loan occurs over 1 day, 2 days, more than 2 days.

In each case make sure you test the condition, use an if statement (not sure how that works in PHP), and does the right
thing in each and every case. The time periods concerned can be done by simply (end time of section - start time of section) in
seconds, and then converted by a factor if needed, remembering to add together all of the chunks if appropriate, if there is only
one piece of time then it is just the one bit, but if there are several then I'm afraid you have go through them somehow and add them.
No maths function is really going to help. So a computer program is needed using if constructs, procedures, method calls,
(or whatever is used in PHP) or whatever is appopriate in the language(s) used.
I suggest setting up variables and constants with meaningful identifiers for ALL of the relevant things to do with time
and rate of charge for each printer.

I am not sure whether you realised that the time stamps appear to be seconds since 01/01/1970.
I apologise if any of that is wrong so double check all of it if it is for a serious purpose like a real business or serious Computer studies.

Last edited by SteveB (2013-07-22 08:21:02)

Offline