How can I make a calendar with MYSQL data? - php

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.

Related

Carbon PHP Loop through each month on date range [duplicate]

I failed to find a proper solution to this issue. As you see in Example #3 in the PHP documentation, they state that one must beware when adding months using the DateInterval in DateTime::add.
There's not really any explanation for why the method's behavior is as such and what I can do to avoid this, which I find to be an error at first sight.
Anyone have some insight into this?
The issue is that each month can have a different number of days in them. The question is what you're doing when you want to increment a date by 1 month. Per the PHP documentation if you're on January 31st (or 30th) and you add 1 month, what is the expected behavior?
February only has 29 days in it. Do you want to be set to the last day of the month? You're generally safer incrementing by a set number of days if that's what you're looking for, or a static date based on the current date. Without knowing what you're trying to accomplish when you increment your month, it's tough to say how to watch for an error.
EDIT:
As someone mentions in the similar post commented by Mike B above, you probably want to do something where you (in pseudocode):
1) Use cal_days_in_month() for the next month and save that number to a variable x
2) If x >= current billing DOB, increment and be done
3) DateTime::modify('last day') (havent used this before but something along these lines) to set the date to the last date of the next month (set it to the 1st of the next month, then last day?)
Worth noting is that if you use the variable here as the new billing value, you'll wipe out your original value. I would save an extra DB value that's "first billing date" or just "billing_day_of_month" or something, and use that to figure out the day of month that you should be looking at
If your goal is to strictly increment by user-friendly months (thus, 3 months from January 21st should be April 21st), with the exception that shorter membership months get shortened (thus, 1 month from January 31st is February 28th/29th), then you only need to go back a few days if you crossed over into the next month:
function addMonths($date,$months) {
$orig_day = $date->format("d");
$date->modify("+".$months." months");
while ($date->format("d")<$orig_day && $date->format("d")<5)
$date->modify("-1 day");
}
$d = new DateTime("2000-01-31");
addMonths($d,1);
echo $d->format("Y-m-d"); // 2000-02-29

What is the best practice in creating scheduling calendar in php and html?

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

PHP MySQL Table w AJAX: Filling in data for the whole year (day by day, only showing 7 days at once)

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);

Complex Date Issue - PHP - Codeigniter - Jquery UI Calendar

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!

What can go wrong when adding months with a DateInterval and DateTime::add?

I failed to find a proper solution to this issue. As you see in Example #3 in the PHP documentation, they state that one must beware when adding months using the DateInterval in DateTime::add.
There's not really any explanation for why the method's behavior is as such and what I can do to avoid this, which I find to be an error at first sight.
Anyone have some insight into this?
The issue is that each month can have a different number of days in them. The question is what you're doing when you want to increment a date by 1 month. Per the PHP documentation if you're on January 31st (or 30th) and you add 1 month, what is the expected behavior?
February only has 29 days in it. Do you want to be set to the last day of the month? You're generally safer incrementing by a set number of days if that's what you're looking for, or a static date based on the current date. Without knowing what you're trying to accomplish when you increment your month, it's tough to say how to watch for an error.
EDIT:
As someone mentions in the similar post commented by Mike B above, you probably want to do something where you (in pseudocode):
1) Use cal_days_in_month() for the next month and save that number to a variable x
2) If x >= current billing DOB, increment and be done
3) DateTime::modify('last day') (havent used this before but something along these lines) to set the date to the last date of the next month (set it to the 1st of the next month, then last day?)
Worth noting is that if you use the variable here as the new billing value, you'll wipe out your original value. I would save an extra DB value that's "first billing date" or just "billing_day_of_month" or something, and use that to figure out the day of month that you should be looking at
If your goal is to strictly increment by user-friendly months (thus, 3 months from January 21st should be April 21st), with the exception that shorter membership months get shortened (thus, 1 month from January 31st is February 28th/29th), then you only need to go back a few days if you crossed over into the next month:
function addMonths($date,$months) {
$orig_day = $date->format("d");
$date->modify("+".$months." months");
while ($date->format("d")<$orig_day && $date->format("d")<5)
$date->modify("-1 day");
}
$d = new DateTime("2000-01-31");
addMonths($d,1);
echo $d->format("Y-m-d"); // 2000-02-29

Categories