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
Related
I working on a website codeigniter based. In my database i have a lot of dates in this format:
yyyy-mm-dd hh:mm:ss
What i want is to represent in a graph, all the 12 months, and for each month the total of dates that i have on my database. For now just a graph for 2012, next year ill change it for 2013.
How can i do it? Any ideas?
I searched a lot for javascript graph/tables such as in google api but none of them gave me everything i needed. Also, how can i read from database the dates and "put them" in each month.
Thanks
At first extract your average dates from Database with for example this code:
SELECT ROUND(AVG(UNIX_TIMESTAMP(`date`)),0) as avg_time
FROM `data`
WHERE `id`>0
GROUP BY Month(`date`)
This SQL works fine and returns average unix_timestamp rounded to the nearest and grouped in Months. In PHP code u can easily cover back to datetime format if needed.
Also u can add in WHERE closure check for year. For example: WHERE YEAR(date)=2012.
Good luck :)
I am developing a holiday guide site!
I am using a MySQL database in which I store the information about hotels.
I have 4 date fields in order to store the 2 possible seasons that works each hotel (date_from, date_to, date_from_extra, date_to_extra). I store the dates in a format like this: DD/MM, for example: 25/12.
I am trying to check if the date1 of first datepicker of the site and the date2 of the second datepicker are between first range (date_from, date_to) or between second range (date_from_extra, date_to_extra) in the database.
The problem is that I don't use years in database date fields and is difficult to compare dates especially when the first or second season in database doesn't finish at the end of the year but it lasts more, for example (14/11 - 25/02).
Any help?
Thanks a lot!
try this
$todayDate = date("Y-m-d");
$check_date= strtotime($todayDate);
use this varible to check
SELECT table_fiels FROM table_namE WHERE month(check_date) BETWEEN month($your_db_start_date) AND month($your_db_end_date) AND day(check_date) BETWEEN day($your_db_start_date) AND day($your_db_end_date)
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"
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.
How do you sort data which was stored in a mysql database depending on the days of the week in which the data was submited ??
I basically want to create a diary which outputs information in each day of the week depending on what day it was posted by dates so,
Mon - Data in order of date
Tue -
Wed - e.t.c
Any code examples and information will be great, thanks.
You can do a
SELECT DAYOFWEEK(datehere) as dayofweek, datehere FROM something ORDER BY dayofweek, datehere;
You can use the DAYOFWEEK function to extract the day, and then sort on it just like any other data.
What kinf of data type is the column where you store the date submission?
It seems like you're asking for a basic SELECT statement?
SELECT some_column, another_colum FROM your_table ORDER BY your_date_column DESC
This assumes you actually have a column that logs the insertion timestamp.
If this answer is obnoxiously simplistic, please forgive me...and give us more details :)
Regards.
If your data is stored as a DATE or DATETIME field, use the DAYOFWEEK or DATE_FORMAT functions to turn it into day name for output, but continue to order by the DATE field
SELECT DATE_FORMAT(my_date_column, '%W') AS dayofweek
FROM my_table
ORDER BY my_date_column
Well, the sorting bit is easy, just sort on the column that represents the post's date. The grouping in days is something you can do in your code, since you need to check the date there anyway (for post-processing the actual output).
To put it this way, you can do a subselect to get the specific day of the week, but in your code you would have to check the day again to group posts per day. In that case it's better (and cleaner, since you're separating business logic from data) to do this in your code, something like this:
select all posts (within date range)
make an associative array, with the
days as keys, and posts (in new
arrays) as values
loop through the
days and then posts to output the
posts per day
SELECT *
FROM diary_entries
ORDER BY FIELD(DAYOFWEEK(created), '2, 3, 4, 5, 6, 7, 1'), created
DAYOFWEEK grabs day of the week number (1 = Sunday).
FIELD makes Monday first day of the week.
After sorting by day of week, then sorted by date created.