Reservation system dates - php

I have problem thinking this trough with PHP & Mysql. I have read How to implement check availability in hotel reservation system and some more but they seem different.
I have to make a reservation system on which a converence room can be booked for 6hours minimal, so when a user make a reservation on a date e.g 24/04/2012 he then selects the start time of the rental (00:00-06:00, 06:00-12:00, 12:00-18:00,18:00-24:00). And the total number of hours(6,12,18 etc..) the user wants to rent the room.
The date is stored as a integer(timestamp).
I have two problems with this; I donĀ“t know how to receive all possible days of a month on which the room is still up for reservation and how to check if a new reservation is possible.
I know how to calculate the start date and end date on the users input but I cant find the correct mysql query or php check for availability.
It's probably really simple but somehow because the end date is always variable I just cant find the answer.
Thanks in advance guys!
EDIT Table structure: reservations
id int(11)
user_id int(11)
reservation_name varchar(255)
start_date int(11)
end_date int(11)
I believe reservations is the only one relevant

You'll find it's pretty difficult to generate a list of available days in MySQL. I recommend instead that you select an ordered list of booked days within your desired month, then loop over all days of that month in PHP skipping the day if it matches the next booked day from your MySQL query. The answer to this question will help you to build the dates over which you want to loop in PHP. In pseudocode:
$booked_days = sql(select all booked days in month order by day);
for each $day in month {
if $day != current($booked_days) {
// $day is not booked
} else advance_next($booked_days);
}
To check if a new reservation is possible, you might want to have a look at my answer to a very similar question earlier today.

This brilliant simple MySQL query must do the trick:
Checking for overlapping car reservations
This solution is only for "dates" but You can simply aggregate it with additional "hours"

Related

Returning a report for each date in a PHP array

What I am trying to do here is render a report for each date in the current month from a MYSQL table.
I have a table with rows in, which a lot have the same date. As you can see here
What I would like to do is for every date in the current month is see how many rows have been found in the MYSQL table for that date and then return it into an array. The final product will be a multidimensional array for every date in the current month. Something like this
array("dates"=>
array(
"1 feb"=>2, //the number of rows for that date found in the MYSQL table
"2 feb"=>8,
"3 feb"=>0
)
)
But the issue is I wouldn't have a clue where to start with the coding, like what PHP functions would I use? So I was hoping someone could push me in the right direction :)
Probably not working code but should point you in the right direction:
SELECT
SUM(money) AS sum,
`date`
FROM
money_table
WHERE
MONTH(`date`) = 2
GROUP BY
DAY(`date`)
Reference
GROUP BY
MONTH
DAY
You said you didn't have a table with all the dates. If you did, this would be trivial. Let's say you had such a table called calendar.
select calendardate
, ifnull(sum(money), 0) sum_of_money
from calendar left join money on calendardate = money.date
where calendardate >= {d '2013-02-01'}
and calendardate < {d '2013-03-01'}
group by calendardate
Other things that could be stored in the calendar table are fiscal information and holidays. It might be worth your while to create and maintain one.

How do I use price data in one table for a calculation that is stored in another table?

I'm still leanring PHP/MySQL but have learned quite a bit thanks to codies on StackOverflow. I'm trying to setup a sort of room reservations system using two tables:
SETUP:
Room price table: Has, prices for a type room a client may want to rent as well as the dates (day of week) they wish to use it. Pricing varies based on day of the week and per room.
I've setup a different table for each room type as each room type carries different pricing for each day of the week. So, There is an Alpha room table, Bravo room, etc. Within Alpha table are headers for the days of the week with pricing pre-entered into the rows.
Client info table: Has the name, address, date of room use, etc data for the specific client.
EXAMPLE:
Alpha-room price table:
Sun = $100; Mon = $200; Tue=$300 and so on.
Bravo-room price table:
Sun = $100; Mon = $200; Tue=$300 and so on.
Client data table:
ClientName; date-of-room-use; address; day_subtotal; grand_total.
QUESTION:
I'm trying to find PHP code that will:
look at the date of room use in the client data table,
look up the associated cost for that date in the specific room pricing table,
record that unit cost in the day subtotal of the client data table
and sum a grand total in the grand total row of the client data
table (assuming the room may be used more than one day by the
customer).
I know there's something to do with join but I'm finding it difficult to grasp the concept and, if someone can demonstrate using this example, I think I will have a better understanding of how to work this sort of transaction.
Thank you ALL in advance for your suggestions or alternatvie approaches.
First, you should separate your database slightly, you should have four tables. rooms, prices, clients and bookings. Setup somewhat like this...
rooms should have the following fields: id, name and description.
prices should have the following fields: id, price, room_id and day.
clients should have the following fields: id and whatever else you want to store on the user, such as first and last names, phone number or whatever.
bookings should have the following fields: id, client_id, room_id, started_at and ended_at. Preferably the started_at and ended_at fields will be an int, filled with PHP's time() method.
You can add any extra fields you want/need to the tables.
With the tables separated out like this, you will be able to properly query the database. So to answer your questions...
look at the date of room in use...
You can now query to see if a room is in use on a specific date by doing the following...
<?php
$selectedDate= mktime(); // Create a UNIX timestamp based on the day the user selected.
$query = "SELECT r.name, r.description
FROM rooms r, bookings b
WHERE b.room_id = r.id
AND b.started_at < $selectedDate
OR b.ended_at > $selectedDate";
$result = $pdo->query($query);
?>
Look up cost for that date
<?php
$selectedDate = mktime() // Create a UNIX timestamp based on the day the user selected
$dayOfWeek = Date('N', $selectedDate); // This will give the numerical day of the week.
$query = "SELECT price
FROM prices
WHERE room_id = $roomId
AND day = $dayOfWeek";
$result = $pdo->query($query);
?>
Record that unit cost in the client table
Doing this is just silly on account of already having the information in another table. Never recreate the same information in a database. If you are, you have built your database incorrectly.
Grand total in the customer table
Again, silly... don't recreate data...
Though, to find that information out, you would first need to do a query on the bookings table, and see the start and end date for which the user will be occupying the room.
Do a calculation on how long the client will be in the room for, (ended_at - started_at) / 86400, (86400 is the number of seconds in a day) that will give the number of days the client is in the room for.
Now that you know which days, and how long the client will be in the room, you can dynamically create a sql call to select the days of the week you need, remember Date('N', $timeStamp) will give you the numerical day of the week for a given timestamp.
Then it is just a matter of doing simple addition.
I have given you the basics here, You can modify the query from answer one to show you if a room is available to be booked within the time frame the user asked for.
I hope that covers everything you asked about...

Pull three upcoming events from MySQL database

I have a database of events that are used to fill a PHP calender. I want to also have a page that lists three upcoming events in date order when ever the page loads. The records have a separate field for day, month and year. The issue i run in to is getting records that accrue after the current date.
Table structure will be good to have but I will guess the query that you need. Use that as a point to start from.
SELECT * FROM events WHERE CONCAT(year,'-',month,'-',date) > DATE_FORMAT(NOW(),'%Y-%m-%d')
ORDER BY year DESC, month DESC, date DESC LIMIT 3;
You are going to have to build a date from your three fields to test against current date, or split current date into year, month, day and test all three against data in your table.
Got to say, not a brill decision on someones part to split date like that.
Need table structure before we can properly help query wise.

Booking query does not consider arrival and departure day

I have been searching for an answer to this dilemma but found nothing. I was hoping you could help to find out what I am doing wrong.
I have a mysql query that selects only apartmens available as follow:
SELECT *
FROM apartments
WHERE apartment_ID NOT IN (SELECT apartment_ID
FROM bookings
WHERE startDate <= '$endingdate'
AND endDate >= '$startingdate')
The problem is that this query is not considering that i.e. departure day is an half day and it is available.
To explain better:
if table "booking" has a booking ending on 16-01-2011 and the search is from the 16-01-2011. The apartment should be available because that is an "half" day (the day of departure). The query seems to not consider this and does not show the apartment.
Please could you help?
Francesco
I'm not that familiar with MySQL data types , but are you sure that startDate and endDate are not stored with a time part as well. If that's the case then "16-01-2011 16:25" would not be less than or equal to "16-01-2011".
Take out the = signs from your sub query, or at least the last one if a place vacated on a date is always available on that date. That should check for apartments being vacated on the requested date. Also make sure that your data is escaped.
If availabilty depends on time then you will need to change your date fields to datetimes.

Does today's date intersect existing ranges in database PHP/MYSQL

I'm creating a time off requester for my business and I think I've run into a small issue.
Goal:
I want to display a page that will show me if anyone has request the current day off.
Problem:
I have the date_from and date_to fields which contain the information of the start and end dates for each request off. However, and I apologize if I'm just over thinking (under thinking?) the solution, but I cannot for the life of me figure out how to compare if today's current date (yyyy-mm-dd) intersects with any of the current records in my database. I've tried toying around with the between query but haven't had much luck.
Any assistance would be greatly appreciated!
SELECT * FROM TheTable
WHERE CURDATE() BETWEEN date_from AND date_to

Categories