working out cost bewteen different dates with different rate

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Feb 19, 2003 2:06 pm

Thu Mar 06, 2003 6:33 am

Hi there

I need some help with some date issues I'm having.

It is for a guesthouse.
There is a summer rate of say $130/per day.
There is a winter rate of say $100/per day.
There is a christmas rate of say $150/per day.
Summer starts on 21/09/year
Winter starts on 21/03/year
christmas is from 15/12/year until 10/01/year.

say a customer stays from 01/12/year to 18/01/year I need to be able to calculate the total amount and be able to run into christmas, pay christmas rate for and then pay summer rate.
the same goes for running across winter to summer or christmas to summer to winter, etc.

any help would be great

New php-forum User
New php-forum User
Posts: 17
Joined: Mon Mar 03, 2003 10:53 am

Sat Mar 08, 2003 7:01 am


I can't give you a complete answer on how to tackle this problem, but I could give you some hints on where to look.

First of all I would advise you to work with the PHP timestamp function.
The UNIX timestamp is the number of seconds elapsed since January 1, 1970 at 00:00:00 - you can print it out like this:
<?php echo time(); ?>
This is easier to use than the date functions because with this timestamp you can easily add/substract values (which in your case is a must).

You would have to check how long somebody stays and in which period their stay falls. The first part of this can be solved easily with the timestamp function. Just a matter of addition and substraction. Although if you use a form for people to post their staying-dates, then you should use a function to calculate the difference. I found this on the web somewhere which should do the trick.


// dates are in the form of: 1978-04-26 02:00:00.

function date_diff($str_start, $str_end)

$str_start = strtotime($str_start); // The start date becomes a timestamp
$str_end = strtotime($str_end); // The end date becomes a timestamp

$nseconds = $str_end - $str_start; // Number of seconds between the two dates
$ndays = round($nseconds / 86400); // One day has 86400 seconds
$nseconds = $nseconds % 86400; // The remainder from the operation
$nhours = round($nseconds / 3600); // One hour has 3600 seconds
$nseconds = $nseconds % 3600;
$nminutes = round($nseconds / 60); // One minute has 60 seconds, duh!
$nseconds = $nseconds % 60;

echo $ndays." days, ".$nhours." hours, ".$nminutes." minutes, ".$nseconds."
echo “seconds<br>\n";


// test the function with several values
date_diff("1978-04-26", "2003-01-01");
date_diff("1984-10-24 15:32:25", "2003-01-01");
date_diff("2001-10-28 17:32:25", "2003-01-01 12:00:18");


To test which period the person stays (so you can calc. their price) ...uh, good luck on that one! :) Can't be too hard though...GOOD luck!


Post Reply