I'm having lots of trouble building an appointment booking site for personal trainers. To understand fully why I'm having so much trouble please be patient and let me explain. I wanted to use this free responsive calendar for the look and feel of my calendar because it's responsive and I like the way it looks, although I'll make some minor alterations to it like color scheme. If you go to the link below you will see a demo of it:
http://tympanus.net/Development/Calendario/index2.html
If you click back on the calendar arrow to November 2012, you will see that certain days have circles on them which indicate that there are events on those days. If you click on the circle a window will come up which is supposed to provide info about the event. The events are fetched from a JSON file whose format is the date for the keys and the values are URLs. When a user clicks on an event instead of the window coming up I want them to be taken to a separate page which will list the available time slots for the trainer on that particular day. Their availability will be based on two things:
1) The schedule that they set for themselves like for instance Mondays, Wednesdays, Fridays from 10am-5pm. I would provide a form on the site where they could set their schedule like this and then save it to a MySQL database and query that database to populate the JSON file.
2) What appointment slots are booked on available days. This is complicated since appointment times can vary. A session can last 15, 30, 45 minutes, 1 hour, 2 hours and 30 minutes, etc. All in 15 minute chunks. So for instance if the session that the user selected is a 1 hour session, I would have to query the schedule of the personal trainer and match that against his appointments booked on every single day and try to figure out where he has 1 hour slots available then show them to the user on a separate page after they've clicked on the calendar date.
I'm having lots of difficulty figuring this out. I can populate the JSON file by querying the schedule of the personal trainer and then using a trick similar to the one below to get all the dates for the next year for a certain day that the trainer is available like for instance Saturday below. Then store that info in the JSON file which will fetch the events for the calendar script:
<?php
$day = 'saturday';
$step = 1;
$unit = 'W';
$start = new DateTime('NOW');
$end = clone $start;
$start->modify($day); // Move to first occurence of day
$end->add(new DateInterval('P1Y')); // Move to 1 year from start
$interval = new DateInterval("P{$step}{$unit}");
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $date) {
echo $date->format('D, d M Y'), PHP_EOL;
echo "<br />";
}
?>
Why would you want to send them to a separate page, when the whole idea of this calendar is to load events inline?
In your json I would only pass the available timeslots for a given day, in order to keep your frontend logic as basic as possible. Figure out serverside what timeslots are still available by walking through that days' events. This is fairly straightforward (sort the appointments, measure the time between the end of event x and the start of event x+1).
Beware of concurrency.
Storing and retrieving schedules and appointments (have you considered recurring appointments too?) can get complicated. You could read up on Martin Fowler, who has an excellent article on modelling schedules and calendars.
Related
I want to create a website with an scheduling calendar.
My first idea is to use some free calendar template or download some free scheduling calendar. Then in my scheduling form, when someone request for an schedule, I will get the date he/she input and save it into the database then show it to the scheduling calendar.
But someone told me that, in my database, I should create a calendar table.
Which is the best way around?
The first one with only one table for schedule on my database or the second one with two tables for schedule and calendar?
I hope you get my idea.
It could be first one. One of option is to keep data by day of year.
you can draw your own calendar by counting day of year
actual day of yaer - date('z') + 1; //+ 1 because it is an array it starts from 0
then you can get number of days in each month
cal_days_in_month
and loop it x 12 with
here will be day of month with css style so it looks like calendar field
your i++ will bee number of days in month of course.
Keep records in database by year and day of year. you can do so much things this way
I’m struggling on how to design this small application. It’s going to be a table info where the columns will be dates and only 7 dates will be shown at any given time (the current day, plus the next six days).
The user NEEDS to be able to scroll to the left (or right, I suppose) and see previous dates with information that has bee entered for specific dates (ie, user wants to see the # of Service Jobs for February 14th, 2015).
What I have so far:
In this case there are 6 rows of information. The user can go in and edit this information. All of this data needs to be posted to a database, and the dates need to account for the whole year (ie, 1/1 to 12/31) and this has to happen every year. I’ve worked with the x-editable plugin a bit and have had some success, but I’m having a difficult time understanding how to account for all the differernt days of the year (365) and how to incorporate that into a MySQL database.
Current'y the 6 dates you see at the top of the img are shown using Javascript. The little dotted lines below the row values are from the X-editable plugin, and when you update this info it is posted to a database with a timestamp.
So my question is: how in the world do I account for 365 days for the year? It's not like I want 365 columns, each representing one day of the year, that seems crazy. (and the same thing will have to happen for 2016, 2017, etc etc...ongoing data).
I'm not sure if I've been taking the right approach on this. Feel like I'm making it much more painful than it needs to be.
Any input on how to design such an application would be greatly appreciated.
From a high level; I would just use an indexed DATE column.
A quick example:
<?php
$date_start = date('Y-m-d'); // Today
$date_end = date('Y-m-d', strtotime($date_start . ' + 6 days'));
// Assuming you have a PDO DB handler setup at this point...
$stmt = $dbh->prepare('SELECT * FROM `table` WHERE `day` BETWEEN ? AND ? ORDER BY `day` ASC');
$stmt->execute(array($date_start, $date_end));
$res = $stmt->fetchAll();
var_dump($res);
I'm trying to create a schedule of unique, recurring events that cycle on a weekly basis (each event will have unique identifiers and repeat weekly).
In addition to other information gathered about each event, I will gather the weekday on which each event will take place and the dates (calendar dates) on which the event will begin and end.
An example:
Event: Go to gym (the event I plan to do once a week)
Day of Week: Sunday (day of week event will occur (every Sunday))
Start Date: 2014-09-01 (happens to be a Monday)
End Date: 2015-08-31 (...)
The purpose is to write a script that takes any multitude of events and their respective weekday/start/end date and recreate a calendar depicting the future calendar dates on which each event will occur.
So the first date I should see scheduled to go to the gym will be:
2014-09-07
because this is first Sunday following the start date.
To sum it up, I am gathering the weekday of each event as well as the start and end dates. How can I parse these pieces of data into something that spits out a list of the events' future dates of reoccurrence?
Keep in mind that there will be a vast number of events that will occur on different days of the week and have different start/end dates.
Thank you for all who read and respond.
Tried:
SQL: grouping, different select statements, and stuff
PHP: stuff, date/time stuff, and stuff stuff
For next event in schedule you could use strtotime function:
strtotime('2014-09-01 next Sunday');
and use returned UNIX time.
But for complete solution of your problem there can be many ways. IMHO, strtotime used in for cycle can give you a large overhead if calculated on demand. If you store schedule in DB, perhaps it will be easiest solution to generete dates and regenerate them on edit.
In other hand, dates are pain in head so algoritm to calculate all ocasions can be much bigger. You can use intervals calculated from difference of result date from first date if $firstDate = $startDate or if event happens every day once a week, then just add 7 days to it.
Post more details or your solution to find mistakes.
I am creating a site that utilizes the jquery ui datepicker. I have it where items have different rental lengths. So a car might be 7 days, while a tent might be 3 days.
When the user enters a quantity of items needed, the system checks if the span of days is available and then puts together an array of dates that are already in the system. This array is passed back to the jquery ui datepicker and those dates are blocked out on the calendar.
Here's my problem. If a user picks a date range that is say, 2 days long. from the 2nd to the 4th of the month. Then a user goes back and wants to choose the 1st, obviously if the item is rented on the 2nd then a 1st to 3rd of the month rental ISNT available.
I'm stumped on how to block out days where the full rental length is not available. Here is what I have so far for the code that creates the blocked date array:
$dateArray = array();
foreach($rentals as $rental) {
//Figure out how many days are between the start and end...
$now = strtotime($rental['periodEnd']);
$your_date = strtotime($rental['periodStart']);
$datediff = $now - $your_date;
$daysBetween = floor($datediff/(60*60*24));
$i = 0;
while($i < $daysBetween) {
if($i != 0) {
$date = date('Y-m-d', strtotime($rental['periodStart']. ' + ' . $i . ' days'));
}else {
$date = date('Y-m-d', strtotime($rental['periodStart']));
}
array_push($dateArray, $date);
$i++;
}
}
//Now get rid of duplicates...
$dateArray = array_unique($dateArray);
echo json_encode($dateArray);
I added this exact feature to a website that offered chartered fishing trips. There were a lot more variables involved in my solution, so this may be somewhat broad/vague, but should at least push you in the right direction.
I set up a callback function for Datepicker's beforeShowDay option:
A function takes a date as a parameter and must return an array with [0] equal to true/false indicating whether or not this date is selectable, [1] equal to a CSS class name or "" for the default presentation, and [2] an optional popup tooltip for this date. It is called for each day in the datepicker before it is displayed.
In my callback, I compare the user's current choices (the trip they chose, which can vary from 1/2 to 3 days, and their chosen departure date) with a list of unavailable dates (which I generate dynamically using PHP & json_encode() like yourself).
When the user opens the Datepicker, the callback will fire for every individual day on the currently-displayed calendar. So each day will go through a logic similar to:
Is this day in the past? (Can't charter a trip in the past!)
Is this day already reserved?
How long is this reservation for? Are there any reserved days in the future that conflict with this timeframe?
Note that in my descriptions, this is all front-end / jQuery based -- current chosen values inputted by the user are pulled using $('#item').val(). The callback has no interactions with the back end. You can use ajax in your callback, but I found it to be much much slower. Your mileage may vary.
Also, you should definitely have a back-end solution that does pretty much the same thing, as well. Don't rely on JS to do your filtering.
Hope that gets you on track!
In my database, there are some days with data and some without; I have one column with data, and another column with the date and time submitted. So I want to create this calendar. So it will show all the days in the month, and then the days with data will be a hyperlink.
When I click the link, it will show all the data submitted on that day. I would probably use a loop within the first loop. One for month, and then one for displaying each and every day.
However, being that each month of the year has different amount of days and also leap year is a problem, I don't know how to write the conditions.
Any help or guidance is appreciated.
$start = '2009-01-01';
$current = strtotime($start);
while(date('n',$current)==date('n',strtotime($start))) {
// do your stuff here, $current includes the current date.
// the loop will run through the complete month
$current = strtotime(date('Y-m-d',$current) . '+1 day');
}
You'll need to first find out what day of the week the month starts, so you know how many empty boxes to spit out. Then figure out how many days in that month. Then loop through your days, wrapping to the next line after Saturday. Then fill in the rest of the last row with empty boxes.
There's some quite simple (and commented) code that does this here:
http://gitorious.org/wfpl/wfpl/blobs/master/calendar.php
You can use that code without the rest of the framework it's designed for, if you simply rewrite calender_day() (which is called for each cell on the calendar, with it's first parameter telling you what sort of day it is (not in month, has events, eventless) and rewrite calendar_week() which is called at the end of each row.
Or you could just look through for how to do relevant stuff, like find out how far through the week the month starts and such.