I have this script. The code below shows the usual calendar
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Monday</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<?php
date_default_timezone_set('Asia/Jakarta');
$days_count = date('t');
$current_day = date('d');
// save time of first day for later use
$time_first_day_of_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
$week_day_first = date('N', $time_first_day_of_month);
// Get next month dates
$next_start = strtotime(date("Y-m-00", strtotime("+1 month")));
$next_dates = array();
for ($w = 1 - $week_day_first + 1; $w <= $days_count; $w = $w + 7){
echo '<tr>';
$counter = 0;
for ($d = $w; $d <= $w + 6; $d++){
if($d < 10){
$current_date = date("Y").date("m").'0'.$d;
}else{
$current_date = date("Y").date("m").$d;
}
echo '<td valign="top" width="14.2857%"'.(($d > 0 ? ($d > $days_count ? ' class="disabled"' : '') : ' class="disabled"')).(($counter > 4 ? ' class="week-day"' : '')).'>';
if($d > 0){
// next month's dates
if($d > $days_count){
for($in = 1; $in <= 1; $in++){
echo array_push($next_dates, date('j', strtotime("+ $in day", $next_start)));
}
}
// today
else if($current_day == $d){
echo '<div class="current-day" style="font-weight: bold;color:red;"><span class="given-date">'.$d.'</span></div>';
}
// this month's dates
else{
echo '<span class="given-date">'.$d.'</span>';
}
}
// last month's dates
else{
//Here comes previous dates
$offset = $d - 1;
echo '<span class="given-date">'.date('d', strtotime("$offset day",$time_first_day_of_month)).'</span>';
}
echo '</td>';
$counter++;
}
echo '</tr>';
}
?>
</table>
I want to create an Islamic calendar like this https://www.islamicfinder.org/islamic-calendar/. How to combine it with islamic calendar? And I do not know how to calculate to get the calendar result of Islam
Related
I have created a table in my MySQL DB which contains the dates for a whole month.
I am then trying to display them in a table (looking like a calendar)
I have created a script, so the first day of the month shows on the right day (eg Saturday for October).
I'm then displaying all other dates after it.
Code
$Firstdate = date('Y-m-01');
$FirstDay = date("N", strtotime($Firstdate));
if ($FirstDay == 1) {}
if ($FirstDay == 2) { echo"<td></td>";}
if ($FirstDay == 3) { echo"<td></td> <td></td>";}
if ($FirstDay == 4) { echo"<td></td> <td></td> <td></td>";}
if ($FirstDay == 5) { echo"<td></td> <td></td> <td></td> <td></td>";}
if ($FirstDay == 6) { echo"<td></td> <td></td> <td></td> <td></td> <td></td>";}
if ($FirstDay == 7) { echo"<td></td> <td></td> <td></td> <td></td> <td></td> <td></td>";}
$result=mysql_query("SELECT * FROM calendar")or die('ERROR 315' );
$num_rows = mysql_num_rows($result);
for ($i = 1; $i <= mysql_num_rows($result); $i++)
{
$row = mysql_fetch_array($result);
$TheDate = $row ['date'];
$TheDateF = date("jS", strtotime($TheDate));
echo " <td> $TheDateF</Center><br><br><br><br></td>";
if ($i % 7 == 0) {
echo '</tr><tr>'; // it's time no move to next row
}
}
This starts on the correct day, however see screenshot below, I need it to start a new row after the Sunday date:
Any ideas how I can resolve this?
Try this code. Modify according to your need. I'm pretty sure this will help you.
<?php
/* Set the default timezone */
date_default_timezone_set("America/Montreal");
/* Set the date */
$date = strtotime(date("Y-m-d"));
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
$firstDay = mktime(0,0,0,$month, 1, $year);
$title = strftime('%B', $firstDay);
$dayOfWeek = date('D', $firstDay);
$daysInMonth = cal_days_in_month(0, $month, $year);
/* Get the name of the week days */
$timestamp = strtotime('next Sunday');
$weekDays = array();
for ($i = 0; $i < 7; $i++) {
$weekDays[] = strftime('%a', $timestamp);
$timestamp = strtotime('+1 day', $timestamp);
}
$blank = date('w', strtotime("{$year}-{$month}-01"));
?>
<table class='table table-bordered' border=1 style="table-layout: fixed;">
<tr>
<th colspan="7" class="text-center"> <?php echo $title ?> <?php echo $year ?> </th>
</tr>
<tr>
<?php foreach($weekDays as $key => $weekDay) : ?>
<td class="text-center"><?php echo $weekDay ?></td>
<?php endforeach ?>
</tr>
<tr>
<?php for($i = 0; $i < $blank; $i++): ?>
<td></td>
<?php endfor; ?>
<?php for($i = 1; $i <= $daysInMonth; $i++): ?>
<?php if($day == $i): ?>
<td><strong><?php echo $i ?></strong></td>
<?php else: ?>
<td><?php echo $i ?></td>
<?php endif; ?>
<?php if(($i + $blank) % 7 == 0): ?>
</tr><tr>
<?php endif; ?>
<?php endfor; ?>
<?php for($i = 0; ($i + $blank + $daysInMonth) % 7 != 0; $i++): ?>
<td></td>
<?php endfor; ?>
</tr>
</table>
Source
http://code.runnable.com/VKxpI5dzCMkTrRq1/simple-calendar-for-php
I'm trying to make a calendar that shows this month, and then fills in the remaining days on the end rows with the dates from the next and previous months.
I managed to make it show next months dates and the thing is the issue is that I need it to show previous months dates as well.
If someone manages to figure out the issues please add what the problem was as well how you managed to fix it, to help me in the future.
I will add a image of the calendar as well.
The script:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Mondag</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<?php
// Get current month dates
$days_count = date('t');
$current_day = date('d');
$week_day_first = date('N', mktime(0, 0, 0, date('m'), 1, date('Y')));
// Get previous month dates
// Get next month dates
$next_start = strtotime(date("Y-m-00", strtotime("+1 month")));
$next_dates = array();
for ($w = 1 - $week_day_first + 1; $w <= $days_count; $w = $w + 7){
echo '<tr>';
$counter = 0;
for ($d = $w; $d <= $w + 6; $d++){
if($d < 10){
$current_date = date("Y").date("m").'0'.$d;
}else{
$current_date = date("Y").date("m").$d;
}
echo '<td valign="top" width="14.2857%"'.(($d > 0 ? ($d > $days_count ? ' class="disabled"' : '') : ' class="disabled"')).(($counter > 4 ? ' class="week-day"' : '')).'>';
if($d > 0){
if($d > $days_count){
for($in = 1; $in <= 1; $in++){
echo array_push($next_dates, date('j', strtotime("+ $in day", $next_start)));
}
}else if($current_day == $d){
echo '<div class="current-day"><span class="given-date">'.$d.'</span></div>';
}else{
echo '<span class="given-date">'.$d.'</span>';
}
}else{
//Here comes previous dates
}
echo '</td>';
$counter++;
}
echo '</tr>';
}
?>
</table>
When you calculated $week_day_first, you can save your time calculated from the first day of the month into a variable.
$time_first_day_of_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
Then you can reuse that later to calculate the offset of days before the first of the month.
date('d', strtotime("$offset day",$time_first_day_of_month))
Putting it all together:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Mondag</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<?php
// Get current month dates
$days_count = date('t');
$current_day = date('d');
// save time of first day for later use
$time_first_day_of_month = mktime(0, 0, 0, date('m'), 1, date('Y'));
$week_day_first = date('N', $time_first_day_of_month);
// Get next month dates
$next_start = strtotime(date("Y-m-00", strtotime("+1 month")));
$next_dates = array();
for ($w = 1 - $week_day_first + 1; $w <= $days_count; $w = $w + 7){
echo '<tr>';
$counter = 0;
for ($d = $w; $d <= $w + 6; $d++){
if($d < 10){
$current_date = date("Y").date("m").'0'.$d;
}else{
$current_date = date("Y").date("m").$d;
}
echo '<td valign="top" width="14.2857%"'.(($d > 0 ? ($d > $days_count ? ' class="disabled"' : '') : ' class="disabled"')).(($counter > 4 ? ' class="week-day"' : '')).'>';
if($d > 0){
// next month's dates
if($d > $days_count){
for($in = 1; $in <= 1; $in++){
echo array_push($next_dates, date('j', strtotime("+ $in day", $next_start)));
}
}
// today
else if($current_day == $d){
echo '<div class="current-day"><span class="given-date">'.$d.'</span></div>';
}
// this month's dates
else{
echo '<span class="given-date">'.$d.'</span>';
}
}
// last month's dates
else{
//Here comes previous dates
$offset = $d - 1;
echo '<span class="given-date">'.date('d', strtotime("$offset day",$time_first_day_of_month)).'</span>';
}
echo '</td>';
$counter++;
}
echo '</tr>';
}
?>
</table>
Results in this output:
<table cellspacing="0" cellpadding="0" border="0" width="100%" class="view-calendar">
<tr>
<td valign="top" width="14.2857%">Mondag</td>
<td valign="top" width="14.2857%">Tuesday</td>
<td valign="top" width="14.2857%">Wednesday</td>
<td valign="top" width="14.2857%">Thursday</td>
<td valign="top" width="14.2857%">Friday</td>
<td valign="top" width="14.2857%">Saturday</td>
<td valign="top" width="14.2857%">Sunday</td>
</tr>
<tr><td valign="top" width="14.2857%" class="disabled"><span class="given-date">30</span></td><td valign="top" width="14.2857%" class="disabled"><span class="given-date">31</span></td><td valign="top" width="14.2857%"><span class="given-date">1</span></td><td valign="top" width="14.2857%"><span class="given-date">2</span></td><td valign="top" width="14.2857%"><span class="given-date">3</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">4</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">5</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">6</span></td><td valign="top" width="14.2857%"><span class="given-date">7</span></td><td valign="top" width="14.2857%"><span class="given-date">8</span></td><td valign="top" width="14.2857%"><span class="given-date">9</span></td><td valign="top" width="14.2857%"><span class="given-date">10</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">11</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">12</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">13</span></td><td valign="top" width="14.2857%"><span class="given-date">14</span></td><td valign="top" width="14.2857%"><span class="given-date">15</span></td><td valign="top" width="14.2857%"><span class="given-date">16</span></td><td valign="top" width="14.2857%"><div class="current-day"><span class="given-date">17</span></div></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">18</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">19</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">20</span></td><td valign="top" width="14.2857%"><span class="given-date">21</span></td><td valign="top" width="14.2857%"><span class="given-date">22</span></td><td valign="top" width="14.2857%"><span class="given-date">23</span></td><td valign="top" width="14.2857%"><span class="given-date">24</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">25</span></td><td valign="top" width="14.2857%" class="week-day"><span class="given-date">26</span></td></tr><tr><td valign="top" width="14.2857%"><span class="given-date">27</span></td><td valign="top" width="14.2857%"><span class="given-date">28</span></td><td valign="top" width="14.2857%"><span class="given-date">29</span></td><td valign="top" width="14.2857%"><span class="given-date">30</span></td><td valign="top" width="14.2857%" class="disabled">1</td><td valign="top" width="14.2857%" class="disabled" class="week-day">2</td><td valign="top" width="14.2857%" class="disabled" class="week-day">3</td></tr></table>
Here is code using the DateTime class, which makes it quite readable:
$today = new DateTime('today'); // only change this line to test other months
$this_month = $today->format('M');
$date = clone $today;
$date->modify('first day of this month')->modify('+1 day')->modify('last Monday');
do {
echo '<tr>';
for ($weekday = 0; $weekday < 7; $weekday++){
$out = str_replace($this_month, '', $date->format('M'));
$content = "<span class='given-date'>$out {$date->format('d')}</span>";
if ($today == $date) $content = "<div class='current-day'>$content</div>";
$class = ($out ? 'disabled ' : '') . ($weekday > 4 ? 'week-day' : '');
echo "<td valign='top' width='14.2857%' class='$class'>$content</td>";
$date->modify('+1 day');
}
echo '</tr>';
} while ($date->format('M') == $this_month);
Although this is quite old now I found Jeff's solution very useful. I refactored it a little and put it in this Gist, I hope someone finds it useful. Part of it is here:
/**
* Return part of an HTML table containing all the days
* in the current month, plus padd it with next and
* previous months days if needed to fill out the grid
*
*
* #param date $date date object containing month to display
*
* #return string
*/
function get_calendar_days_in_month_html($date) {
$date_format = "Y-m-d";
$days_count = $date->format('t'); //Get number of days in this month
$weeks_count = ceil($days_count / 7); //How many weeks in this month?
$total_cells = $weeks_count * 7;
//clone is used or we literally are modifying the $date variable
$first_date_of_month = clone $date->modify('first day of this month');
$first_day_of_month = $first_date_of_month->format("N"); //returns 1-7 EG Mon-Fri
$first_date_of_grid = $first_date_of_month ->modify('-' . $first_day_of_month . ' days');
$todays_date = new DateTime();
$todays_date_str = $todays_date->format($date_format);
$selected_date_str = $date->format($date_format);
$day_of_week = 1; //FIXME: allow starting with Sunday or whatever
$ht = '<tr>';
for ($cell=1; $cell <= $total_cells ; $cell++) {
$classes = []; //CSS classes
$current_date = $first_date_of_grid->modify("+1 day");
$current_date_str = $current_date->format($date_format);
if($current_date_str == $todays_date_str) $classes[] = "today";
if($selected_date_str == $todays_date_str) $classes[] = "selected-date";
/* if current date is not from this month (EG previous or next month) then give
it a special class, you might want to grey it out or whatever */
if($date->format("m") !== $current_date->format("m")) $classes[] = "grid-filler";
$ht .= '
<td date="' . $current_date_str . '" class="' . implode(" ", $classes) . '">' . $current_date->format("j") . '</td>';
$day_of_week ++;
if($day_of_week == 8) {
$ht .= '</tr>';
if($cell < $total_cells) $ht .= '<tr>';
$day_of_week = 1;
}
}
return $ht;
}
I am creating an events calendar for my website and I have used this tutorial and this other one, as well as the youtube tutorial by unknown ghost.
However, when I enter an event, it comes up as successful but doesn't turn blue.
Have I made an error/typo that could explain why it isn't turning blue?
Also once an event has been added, it does display at the bottom but if I refresh the page the event gets added again automatically.
Went through each month and realised February doesn't come up. Why is this?
Here is my code:
calendar.php
<?php
include("functions.php")
?>
<html>
<head>
<title>Event Calendar</title>
<script>
function goLastMonth(month, year){
if(month == 1) {
--year;
month = 13;
}
--month
var monthstring = ""+month+"";
var monthlength = monthstring.length;
if(monthlength <=1){
monthstring = "0"+monthstring;
}
document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
}
function goNextMonth(month, year){
if(month == 12) {
++year;
month = 0;
}
++month
var monthstring = ""+month+"";
var monthlength = monthstring.length;
if(monthlength <=1){
monthstring = "0"+monthstring;
}
document.location.href = "<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
}
</script>
<style>
.today{
background-color: #00ff00;
}
.event{
background-color: #0000ff;
}
</style>
</head>
<body>
<?php
//get current date or specific month and year
if (isset($_GET['day'])){
$day = $_GET['day'];
} else {
$day = date("d");
}
if(isset($_GET['month'])){
$month = $_GET['month'];
} else {
$month = date("m");
}
if(isset($_GET['year'])){
$year = $_GET['year'];
}else{
$year = date("Y");
}
//get date data for display such as month name
$currentTimeStamp = strtotime( "$day-$month-$year");
$monthName = date("F", $currentTimeStamp);
//get the number of days in the current month and year
$numDays = date("t", $currentTimeStamp);
//keep track of the number of cell created
$counter = 0;
?>
<?php
if(isset($_GET['add'])) {
$name = $_POST['Event_Name'];
$details = $_POST['Details'];
$date = $day."/".$month."/".$year;
$location = $_POST['Location'];
$staffrq = $_POST['Staff_Required'];
$cadetsrq = $_POST['Cadets_Required'];
//insert into database
$sqlinsert = "INSERT INTO Event_Detail (Event_Name, Details, Date, Location, Staff_Required, Cadets_Required) VALUES
('".$name."', '".$details."', '".$date."', '".$location."', '".$staffrq."','".$cadetsrq."')";
$resultinsert = mysql_query($sqlinsert);
if($resultinsert) {
echo "Event Successfully Added...";
}else{
echo "Event Failed to be Added...";
}
}
?>
<table border="1px">
<tr>
<td align="center">
<input style='width:80px;' type='button' value='<'name='previousbutton' onclick ="goLastMonth(<?php echo $month.','.$year?>)">
</td>
<td align="center" colspan="5"><?php echo $monthName." ".$year; ?></td>
<td align="center">
<input style='width:80px;' type='button' value='>'name='nextbutton' onclick ="goNextMonth(<?php echo $month.','.$year?>)">
</td>
</tr>
<tr>
<td align="center" width='80px'>Monday</td>
<td align="center" width='80px'>Tuesday</td>
<td align="center" width='80px'>Wednesday</td>
<td align="center" width='80px'>Thursday</td>
<td align="center" width='80px'>Friday</td>
<td align="center" width='80px'>Saturday</td>
<td align="center" width='80px'>Sunday</td>
</tr>
<tr align="center">
<?php
for($i = 1; $i < $numDays+1; $i++, $counter++){
$timeStamp = strtotime("$i-$month-$year");
//create empty cell until first day of the month
if($i == 1) {
//0=sun, 1=mon, 2=tue, ...
$firstDay = date("w", $timeStamp);
//if sunday change the firstDay to 7
if($firstDay == 0)
$firstDay = 7;
//decrement firstDay by 1
$firstDay--;
for($j = 0; $j < $firstDay; $j++, $counter++) {
echo "<td> </td>";
}
}
//create new row
if($counter % 7 == 0) {
echo"</tr><tr>";
}
$monthstring = $month;
$monthlength = strlen($monthstring);
$daystring = $i;
$daylength = strlen($daystring);
if($monthlength <=1){
$monthstring = "0".$monthstring;
}
if($daylength <=1) {
$daystring = "0".$daystring;
}
$todaysDate = date("d/m/Y");
$dateToCompare = $daystring.'/'.$monthstring.'/'.$year;
//print day number
echo "<td align='center'";
if ($todaysDate == $dateToCompare) {
echo "class='today'";
}else{
$sqlCount = "SELECT * FROM Event_Detail WHERE Date='".$dateToCompare."'";
$noOfEvent = mysql_num_rows(mysql_query($sqlCount));
if($noOfEvents >=1) {
echo "class='event'";
}
}
echo "><a href='".$_SERVER['PHP_SELF']."?day=".$daystring."&month=".$monthstring."&year=".$year."&v=true'>".$i."</a></td>";
}
//fill up the leftover cells of the table
while($counter % 7 != 0) {
echo "<td> </td>";
$counter++;
}
?>
</tr>
</table>
<?php
if(isset($_GET['v'])){
echo "<a href='".$_SERVER['PHP_SELF']."?day=".$day."&month=".$month."&year=".$year."&v=true&f=true'>Add Event</a>";
if(isset($_GET['f'])){
include("eventform.php");
}
$sqlEvent = "SELECT * FROM Event_Detail WHERE Date='".$day."/".$month."/".$year."'";
$resultEvents=mysql_query($sqlEvent);
echo "<hr>";
while($events=mysql_fetch_array($resultEvents)){
echo "Event Name: ".$events['Event_Name']."<br>";
echo "Details: ".$events['Details']."<br>";
echo "Date: ".$events['Date']."<br>";
echo "Location: ".$events['Location']."<br>";
echo "Staff Required: ".$events['Staff_Required']."<br>";
echo "Cadets Required: ".$events['Cadets_Required']."<br>";
}
}
?>
</body>
</html>
eventform.php
<title>Event</title>
<form name="eventform" method="POST" action="<?php $_SERVER['PHP_SELF']; ? >?day=<?php echo $day;?>&month=<?php echo $month;?>&year=<?php echo $year;? >&v=true&add=true">
<table width="400px">
<tr>
<td width="150px">Event Name</td>
<td width="250px"><input type="text" name="Event_Name" required></td>
</tr>
<tr>
<td width="150px">Details</td>
<td width="250px"><textarea name="Details" required></textarea></td>
</tr>
<tr>
<td width='150px'>Location</td>
<td width='250px'><textarea name="Location" required></textarea></td>
</tr>
<tr>
<td width='150px'>Staff Required</td>
<td width='250px'><input type='number' name="Staff_Required" required></td>
</tr>
<tr>
<td width='150px'>Cadets Required</td>
<td width='50px'><input type='number' name="Cadets_Required" required></td>
</tr>
<tr>
<td colspan='2' align='center'><input type='submit' name='btnadd' value='Add Event'></td>
</tr>
I have php to display the calender and another php to check database to see bookings, If there is a booking on certain day it displays "Booked". In database i have column for date of booking and title. How could i make it to display Title from the database instead of "Booked"?
This is to display calender.
<?php
$monthNames = Array("January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December");
?>
<?php
if (!isset($_REQUEST["month"]))
$_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"]))
$_REQUEST["year"] = date("Y");
?>
<?php
$cMonth = $_REQUEST["month"];
$cYear = $_REQUEST["year"];
$prev_year = $cYear;
$next_year = $cYear;
$prev_month = $cMonth-1;
$next_month = $cMonth+1;
if ($prev_month == 0 )
{
$prev_month = 12;
$prev_year = $cYear - 1;
}
if ($next_month == 13 )
{
$next_month = 1;
$next_year = $cYear + 1;
}
require("BookingsDB.php");
$myBookingsDB = new BookingsDB();
$bookings = $myBookingsDB->getMonthlyBookings($cMonth,$cYear);
?>
<div align="left">
<table width="400" border="5" align="left" id="calendar">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF"><table width="100%" border="0"
cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="left"><a href="<?php echo $_SERVER["PHP_SELF"]
. "?month=". $prev_month . "&year=" . $prev_year; ?>"
style="color:#FFFFFF">Previous</a></td>
<td width="50%" align="right"><a href="<?php echo $_SERVER["PHP_SELF"]
. "?month=". $next_month . "&year=" . $next_year; ?>"
style="color:#FFFFFF">Next</a></td>
</tr>
</table></td>
</tr>
<tr>
<td align="center"><table width="100%" border="2" cellpadding="2"
cellspacing="2">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong> <?php
echo $monthNames[$cMonth-1].' '.$cYear; ?></strong></td>
</tr>
<tr>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>S</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>M</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>W</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>T</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>F</strong></td>
<td align="center" bgcolor="#999999" style="color:#FFFFFF">
<strong>S</strong></td>
</tr>
<?php
require("connection.php");
$con=mysqli_connect("$mysql_host","$mysql_user","$mysql_password","$mysql_database");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= "select activityEvent.activityTitle from activityEvent";
$result = mysqli_query($con,$sql);
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
$today = getdate();
for ($i=0; $i<($maxday+$startday); $i++)
{
if(($i % 7) == 0 )
{
echo "<tr> ";
}
if($i < $startday)
{
echo "<td></td> ";
}
else
{
$day = $i - $startday + 1;
$thisDate = new DateTime("$cYear-$cMonth-$day");
$jsEvent[] = "document.getElementById('trigger" . $i . "').onclick = function() {showForm()};";
echo "<td align='center' valign='middle' height='20px'><a href='#' id='trigger" . $i . "'>". ($i - $startday + 1) . "</a>";
foreach ($bookings as $bookingDate)
{
if ($thisDate==$bookingDate)
{
echo "Booked";
while($row = mysqli_fetch_array($result))
{
echo $sql= "select activityEvent.activityTitle from activityEvent";
}
}
}
echo "</td>";
}
if(($i % 7) == 6 )
{
echo "</tr> ";
}
}
mysqli_close($con);
?>
<script type="text/javascript">
<?php foreach($jsEvent as $event)
{
echo $event;
}
?>
function showForm(){
document.getElementById('timeslots').style.display="block";
};
</script>
</table></td>
</tr>
</table>
This PHP checks booked dates in the database.
<?php
class BookingsDB
{
private $bookings = array();
private $con;
public function BookingsDB()
{
require("connection.php");
$this->con = mysqli_connect($mysql_host,$mysql_user,$mysql_password,$mysql_database);
}
public function getMonthlyBookings($thisMonth,$thisYear)
{
$sql = "SELECT date,activityTitle FROM activityEvent WHERE YEAR(date) = $thisYear AND MONTH(date) = $thisMonth";
$result = mysqli_query($this->con,$sql);
while($row=mysqli_fetch_array($result))
{
$thisBooking = $row['date'];
$thisBookingDateTime = new DateTime($thisBooking);
array_push($this->bookings,$thisBookingDateTime);
}
return $this->bookings;
}
public function close()
{
mysqli_close($this->con);
}
}
?>
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
.red
{
color: red
}
.green
{
background-color: aquamarine;
}
table
{
text-align: center;
}
td
{
height: 60px;
width: 100px;
}
.opcity
{
background-color: rgba(0,0,0,.2);
}
</style>
</head>
<body>
<?php
$Cur_day = date('d');
$Cur_Month = date('m');
$Cur_Year = date('Y');
$op = '';
if (isset($_REQUEST['month']) || isset($_REQUEST['year']) && isset($_REQUEST['stday']))
{
$Mo = $_REQUEST['month'];
$Year = $_REQUEST['year'];
$query_date = $Year . '-' . $Mo . '-21';
if (isset($_REQUEST['opra']))
{
$str = $_REQUEST['opra'];
if ($str == 'iny')
{
$op = '+1 year';
}
if ($str == 'dey')
{
$op = '-1 year';
}
if ($str == 'inm')
{
$op = '+1 month';
}
if ($str == 'dem')
{
$op = '-1 month';
}
if ($str == 'dem' || $str == 'inm' || $str == 'dey' || $str == 'iny')
{
$query_date1 = date('Y-m-01', strtotime($op, strtotime($query_date)));
$lastDay = date('t', strtotime($op, strtotime($query_date)));
$year = date('Y', strtotime($op, strtotime($query_date)));
$month = date('n', strtotime($op, strtotime($query_date)));
$Month_Name = date('M', strtotime($op, strtotime($query_date)));
$pre_month = date('t', strtotime('-1 month', strtotime($year . '-' . $month . '-21')));
}
if ($str == 'No')
{
$query_date1 = date('Y-m-01', strtotime($query_date));
$lastDay = date('t', strtotime($query_date));
$year = date('Y', strtotime($query_date));
$month = date('n', strtotime($query_date));
$Month_Name = date('M', strtotime($query_date));
$pre_month = date('t', strtotime('-1 month', strtotime($year . '-' . $month . '-21')));
}
}
?>
<input type="hidden" name="txt_year" id="txt_year1" value="<?php echo $year; ?>">
<input type="hidden" name="txt_month1" id="txt_month1" value="<?php echo $month; ?>">
<table border="1">
<tr>
<td><<</td>
<td><</td>
<td colspan="3"><?php echo $Month_Name . "-" . $year; ?></td>
<td>></td>
<td>>></td>
</tr>
<tr>
<?php
echo '<tr>';
for ($i = 1; $i <= 7; $i++)
{
if ($_REQUEST['stday'] == 'Sun')
{
$j_val = 0;
echo "<th>" . date("l", gmmktime(0, 0, 0, 11, $i, 2015));
echo '</th>';
} else
{
$j_val = 1;
echo "<th>" . date("l", gmmktime(0, 0, 0, 6, $i, 2015));
echo '</th>';
}
}
echo '</tr>';
?>
</tr>
<?php
$Conn = new mysqli("localhost", "root", "", "demo");
$firstDays = mktime(0, 0, 0, date($month), 1, date($year));
$weekday = date('N', $firstDays);
$i = 0;
$j = 0;
$date = 1;
$Count = 0;
$Count1 = 0;
$next_mo = 1;
function Holiday($day1, $month1, $year1)
{
global $Conn;
$result = $Conn->query("SELECT * FROM holiday");
while ($row = mysqli_fetch_assoc($result))
{
if ($row['day'] == $day1 && $row['month'] == $month1 && $row['year'] == $year1)
{
return $row['title'] . "<br>";
}
}
}
for ($i = 0; $i < 6; $i++)
{
echo '<tr>';
$pre_month = $pre_month - ($weekday - $j_val);
for ($j = $j_val; $j <= 6 + $j_val; $j++)
{
if ($Count < $weekday - $j_val && ($weekday != 7 || $_REQUEST['stday'] == 'Mon'))
{
echo "<td class='opcity'>" . ( ++$pre_month) . "</td>";
$Count++;
} else
{
if ($date <= $lastDay)
{
if ($Cur_day == $date && $Cur_Month == $month && $Cur_Year == $year)
{
if (date("l", gmmktime(0, 0, 0, $month, $date, $year)) == 'Sunday')
{
echo '<td class = "green red">';
echo Holiday($date, $month, $year);
} else
{
echo '<td class = "green">';
echo Holiday($date, $month, $year);
}
} else
{
if (date("l", gmmktime(0, 0, 0, $month, $date, $year)) == 'Sunday')
{
echo '<td class = "red">';
echo Holiday($date, $month, $year);
} else
{
echo '<td >';
echo Holiday($date, $month, $year);
}
}
echo $date;
echo '</td>';
$date++;
if ($date == $lastDay)
{
$i = 7;
}
} else
{
echo "<td class='opcity'>" . $next_mo++ . "</td>";
}
}
}
echo '</tr>';
}
}
?>
</table>
</body>
</html>
//<Ajax_cal.php
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script src="JS/jquery-1.11.3.min.js" type="text/javascript"></script>
<script>
function Caleonload()
{
$('#month').val('<?php echo date("n"); ?>');
$('#year').val('<?php echo date('Y'); ?>');
Opretion('No');
}
function Cal(month)
{
$('#month').val(month);
Opretion('No');
}
function Cal1(year)
{
$('#year').val(year);
Opretion('No');
}
function Opretion(Op)
{
var st_day = $('input[type="radio"][class="rd_sun_mon"]:checked').val();
var Mo = $('#month').val();
var Year = $('#year').val();
var Op1 = Op;
$.post("AJAX/Ajax_Cal.php",
{
month: Mo,
year: Year,
opra: Op1,
stday: st_day
},
function (data)
{
$('#Show_Cal').html(data);
var txt_year = $('#txt_year1').val();
$("#year").html('');
for (var y = parseInt(txt_year) - 5; y < parseInt(txt_year) + 5; y++)
{
$("#year").append('<option value="' + y + '">' + y + '</option>');
}
$('#year').val(txt_year);
$('#month').val($('#txt_month1').val());
}
);
}
function incyear()
{
Opretion('iny');
}
function deyear()
{
Opretion('dey');
}
function incmonth()
{
Opretion('inm');
}
function demonth()
{
Opretion('dem');
}
</script>
</head>
<body onload="Caleonload()">
<select name="month" id="month" onchange="Cal(this.value)" size="1">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
<select name="year" id="year" onchange="Cal1(this.value)" size="1">
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
</select>
Starting Day :
<input type="radio" name="rd_sun_mon" class="rd_sun_mon" value="Sun" checked="" onchange="Opretion('No')">Sunday
<input type="radio" name="rd_sun_mon" class="rd_sun_mon" value="Mon" onchange="Opretion('No')">Monday
<div id="Show_Cal"></div>
</body>
</html>
DATA BASE PHP MY ADDMIN:
CREATE TABLE IF NOT EXISTS holiday (
id int(5) NOT NULL AUTO_INCREMENT,
day int(4) NOT NULL,
month int(4) NOT NULL,
year int(6) NOT NULL,
title varchar(20) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;
I think your problem is that is that you need to put the format in order to compare two Datetime :
if ($thisDate->format('Y-m-d')==$bookingDate->format('Y-m-d'))
{
echo "Booked";
....
}
In addition to #SimonGuichard's answer, I suggest that you return an associative array from the getMonthlyBookings function that contains both the date and the activityTitle. This way, you wouldn't have to select the title from the database again when it needs to be displayed.
public function getMonthlyBookings($thisMonth,$thisYear) {
$sql = "SELECT date,activityTitle FROM activityEvent WHERE YEAR(date) = $thisYear AND MONTH(date) = $thisMonth";
$result = mysqli_query($this->con,$sql);
while($row=mysqli_fetch_array($result)) {
$this->bookings[] = array('date'=>new DateTime($row['date']), 'activityTitle'=>$row['activityTitle']);
}
return $this->bookings;
}
Then in the script that displays the calendar, you should modify it this way:
$day = $i - $startday + 1;
$thisDate = new DateTime("$cYear-$cMonth-$day");
$jsEvent[] = "document.getElementById('trigger" . $i . "').onclick = function() {showForm()};";
echo "<td align='center' valign='middle' height='20px'><a href='#' id='trigger" . $i . "'>". ($i - $startday + 1) . "</a>";
// $bookings is an associative array of the form array('0'=>array('date'=>'date_value', 'activityTitle'=>'activityTitle_value'))
foreach ($bookings as $key => $value) {
if ($thisDate->format('Y-m-d') === $value['date']->format('Y-m-d')) {
// this prints out the activity title
echo $value['activityTitle'];
}
}
I'm a beginner at programming and I have to build a website for my school project.
With some help I finally built a calendar: yeey!
But now I have to navigate through my calendar: go through the months and years.
For example, today it is 15 January 2015, I want to go to the year 2016, or to 26 January. I really don't know how to do this...:(
Can somebody help me out please?? I will appreciate it:)
The code of my calendar:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
setlocale(LC_ALL, 'nl_NL');
$today_date = time();
$day = date('d', $today_date);
$month = date('m', $today_date);
$year = date('Y', $today_date);
// Make first day of the month
$first_day = mktime(0,0,0,$month, 1, $year);
// Get name of the month
$title = date('F', $first_day);
// What day of the week is the first day of the month
$day_of_week = date('D', $first_day);
// Lege plekken invullen
switch($day_of_week) { case "Sat": $blank = 6; break;
case "Sun": $blank = 0; break;
case "Mon": $blank = 1; break;
case "Tue": $blank = 2; break;
case "Wed": $blank = 3; break;
case "Thu": $blank = 4; break;
case "Fri": $blank = 5; break;
}
// hoeveel dagen in een maand
$days_in_month = cal_days_in_month(0, $month, $year);
// Bovenkant
echo '<table border="0" cellspacing="0" cellpadding="0" width="1500px"> ';
echo '<tr><th colspan="7" class = "monthname"> ' .$title . ' ' . $year. ' </th></tr>';
echo '<tr> <td width="50" class="weekend">Zondag</td>
<td width="50" class = "dag">Maandag</td>
<td width="50" class = "dag">Dinsdag</td>
<td width="50" class = "dag">Woensdag</td>
<td width="50" class = "dag">Donderdag</td>
<td width="50" class = "dag">Vrijdag</td>
<td width="50" class="weekend">Zaterdag</td>
</tr>';
$day_count = 1;
echo '<tr>';
// De dagen die er in een maand niet zijn invullen met een leeg vak
while ( $blank > 0 ) {
if ($day_count == 1 || $day_count == 7) {
echo '<td class="weekend"> </td>';
} else
{echo '<td></td>'; }
$blank = $blank - 1;
$day_count++;
}
$day_num = 1;
while ($day_num <= $days_in_month) { if ($day_count == 1 || $day_count == 7) { if ($day_num == $day) { echo ' <td class="weekend today">
' .$day_num. '
</td>'; } else { echo '
<td class="weekend">
' .$day_num. '
</td>'; } } else { if ($day_num == $day) { echo '
<td class="today">
' .$day_num. '
</td>'; } else { echo '
<td>
' .$day_num. '
</td>'; } } $day_num++; $day_count++;
// Seperate the week out onto new lines
if ($day_count > 7) { echo '</tr>
<tr>
'; $day_count = 1; } }
// Blank out days not needed at the end of the month
while ($day_count > 1 && $day_count <= 7) { if ($day_count == 1 || $day_count == 7) { echo '<td class="weekend">
</td>'; } else
{ echo '<td>
</td>'; } $day_count++; }
// End the table
echo '</tr></table>';
?>
</div>
</body>
</html>
you can do this with 2 Get Variables. m (month) and y (year).
change your rows:
$month = date('m', $today_date);
$year = date('Y', $today_date);
into:
$month = (isset($_GET['m'])) ? $_GET['m'] : date('m', $today_date);
$year = (isset($_GET['y'])) ? $_GET['y'] : date('Y', $today_date);
Now you can call your script with month and year:
?m=3&y=2014
is your view for march 2014.
And you can create Next and Previous links which calling your script with the y and m Get Variables.
Try to use mktime() instead of time().
The parameters for the mktime function should than be transmitted threw the GET variables.