How to show calendar month's previous days? - 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;
}

Related

How to add an Islamic calendar to a public calendar?

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

Event calendar not showing February

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>

select next event from database

I am working on a function which selects a list of dates, and highlight the next event. I have managed to highlight all future events, however I would like to only highlight the nearest future event. How can this be done?
<?php
function racesbydate($sql) {
include 'connect.php';
$year = $_GET['year'];
$get = $year.'%';
$select = $conn->prepare($sql);
$select->bind_param('s', $get);
$select->execute();
$select->store_result();
if ($select->num_rows > 0) {
echo "<div class='races' id='$year' style='display:block'>
<table>\n<th colspan='5'>Cycling Season $year </th>\n
<tr id='information'>\n
<th id='date'>Date</th>\n
<th id='race'>Race</th>\n
<th id='route'>Route</th>\n
<th id='info'>Entry</th>\n
<th id='rizultz'>Results</th>";
$meta = $select->result_metadata();
while ($field = $meta->fetch_field()) {
$params[] = &$row[$field->name];
}
call_user_func_array(array($select, 'bind_result'), $params);
while ($select->fetch()) {
$today = date("Y-m-d");
$date = new DateTime($row['date']);
$formatted_date = $date->format('d/m/Y');
if ($row['date'] > $today) {
if ($row['infoID'] != "0") {
echo "<tr class='racedetails' id='nextrace' >\n
<td class='dark' id='date'>".$formatted_date."</td>\n
<td class='light' id='race'>".$row["title"]."</td>\n
<td class='dark' id='route'>".$row["route"]."</td>\n
<td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
<td class='dark' id='rizults'>N/A</td>\n</tr>\n";
}
} else {
if (($row['infoID'] != "0") && ($row['resultID'] !="0" )) {
echo "<tr class='racedetails' id='race' >\n
<td class='dark' id='date'>".$formatted_date."</td>\n
<td class='light' id='race'>".$row["title"]."</td>\n
<td class='dark' id='route'>".$row["route"]."</td>\n
<td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
<td class='dark' id='rizults'><a href='".$row["resultID"]."'>Results</a></td>\n</tr>\n";
}
}
}
}
echo "</table>\n</div>";
$select->close();
}
current result
<tr class='racedetails' id='nextrace' ><td class='dark' id='date'>06/01/2016</td></tr>
<tr class='racedetails' id='nextrace' ><td class='dark' id='date'>08/02/2016</td></tr>
goal results
<tr class='racedetails' id='nextrace' ><td class='dark' id='date'>06/01/2016</td></tr>
<tr class='racedetails' id='race' ><td class='dark' id='date'>08/02/2016</td></tr>
If I understood you correctly, you only need a flag to store the information whether the next race has already been seen or not:
:
call_user_func_array(array($select, 'bind_result'), $params);
$nextRace = true;
while ($select->fetch()) {
$today = date("Y-m-d");
$date = new DateTime($row['date']);
$formatted_date = $date->format('d/m/Y');
if ($row['date'] > $today) {
if ($row['infoID'] != "0") {
$cssId = $nextRace ? 'nextrace' : 'race';
$nextRace = false;
echo "<tr class='racedetails' id='".$cssId."' >\n
<td class='dark' id='date'>".$formatted_date."</td>\n
<td class='light' id='race'>".$row["title"]."</td>\n
<td class='dark' id='route'>".$row["route"]."</td>\n
<td class='light' id='info'><a href='".$row["infoID"]."'>Info</a></td>\n
<td class='dark' id='rizults'>N/A</td>\n</tr>\n";
}
} else {
:
This assumes that the order of the races is ascending by date (which your examples suggest).

Cannot black out previous dates in my calendar - PHP

I am using a calendar in which i am having trouble making the previous dates unavailable. At the moment my calendar ha links for each date and when you click a date it shows the value on another page. However i need all make all the dates that have already passed unavailable. I know it is something to do with the if statement near the end of the code but i can't figure it out. Here is my code
<?php
$monthNames = Array("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");
?>
<?php
$cMonth = isset($_REQUEST["month"]) ? $_REQUEST["month"] : date("n");
$cYear = isset($_REQUEST["year"]) ? $_REQUEST["year"] : date("Y");
$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;
}
?><!DOCTYPE html>
<html>
<head>
<title>Hook Up</title>
</head>
<style type="text/css">
table {
border: 1px solid black;
border-collapse: collapse;
}
th {
border: 1px solid black;
padding: 6px;
font-weight: bold;
background: #ccc;
}
td {
border: 1px solid black;
padding: 6px;
vertical-align: top;
width: 100px;
}
</style>
<script type="text/javascript">
function eventWindow(url) {
event_popupWin = window.open(url, 'event',
'resizable=yes, scrollbars=yes, toolbar=no, width=400, height=400);
event_popupWin.opener = self;
}
</script>
<body>
<h1>Select a Night Out</h1>
<table width="200">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="left"> Previous</td>
<td width="50%" align="right">Next </td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" border="0" 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
$timestamp = mktime(0,0,0,$cMonth,1,$cYear);
$maxday = date("t",$timestamp);
$thismonth = getdate ($timestamp);
$startday = $thismonth['wday'];
$today = date('j');
$currentmonth = date('n');
for ($i=0; $i<($maxday+$startday); $i++) {
if(($i % 7) == 0 ){
echo "<tr>";
}
if($i < $startday){
echo ("<td class='cell cell_txt'> </td>");
} else {
if (($i - $startday + 1) == $today && $currentmonth == $cMonth){
echo ("<td class='cell_today cell_txt'>".($i-$startday+ 1)."</td>");
} else {
echo ("<td class='cell cell_txt'>".($i - $startday + 1)."</td>");
}
}
if(($i % 7) == 6 ) {
echo "</tr>\n";
}
}
?>
</table>
</td>
</tr>
</table>
</body>
</html>
If anyone can help it would be greatly appreciated. Cheers
if you add following line
$cDay = isset($_REQUEST["day"]) ? $_REQUEST["day"] : date("d");
below this line
$cYear = isset($_REQUEST["year"]) ? $_REQUEST["year"] : date("Y");
and change this line
if (($i - $startday + 1) == $today && $currentmonth == $cMonth){
to:
if ((($i - $startday + 1) == $today && $currentmonth == $cMonth) OR ( (($i - $startday + 1) == $cDay) && ($currentmonth == $cMonth)) ){
That will black out the date that is passed in.
If you want to pass in multiple dates, i.e. pick one date 2013-01-18, the page reloads and blanks out the 18th then want to pick a second date e.g. 2013-01-22 and have the page reload and blank out both the 18th and the 22nd then you will need to change the inputs to arrays and add the previous selected dates into hidden fields to be resubmitted.
If you just want to black out one date then the code changes will work.
Hope this helps.
Update to black out all previous date to the selected:
<?php
$monthNames = Array("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");
?>
<?php
$sMonth = isset($_REQUEST["smonth"]) ? $_REQUEST["smonth"] : date("n");
$sYear = isset($_REQUEST["syear"]) ? $_REQUEST["syear"] : date("Y");
$cMonth = isset($_REQUEST["month"]) ? $_REQUEST["month"] : '';
$cYear = isset($_REQUEST["year"]) ? $_REQUEST["year"] : '';
$cDay = isset($_REQUEST["day"]) ? $_REQUEST["day"] : '';
//echo __line__." Dates in ".$cDay." ".$cMonth.", ".$cYear."<br>";
$prev_year = $sYear;
$next_year = $sYear;
$prev_month = $sMonth - 1;
$next_month = $sMonth + 1;
if ($prev_month == 0) {
$prev_month = 12;
$prev_year = $sYear - 1;
}
if ($next_month == 13) {
$next_month = 1;
$next_year = $sYear + 1;
}
$nextPrevString = "&month=$cMonth&year=$cYear&day=$cDay";
$selectString = "&smonth=$sMonth&syear=$sYear";
?><!DOCTYPE html>
<html>
<head>
<title>Hook Up</title>
</head>
<style type="text/css">
table {
border: 1px solid black;
border-collapse: collapse;
}
th {
border: 1px solid black;
padding: 6px;
font-weight: bold;
background: #ccc;
}
td {
border: 1px solid black;
padding: 6px;
vertical-align: top;
width: 100px;
}
</style>
<script type="text/javascript">
function eventWindow(url) {
event_popupWin = window.open(url, 'event', resizable=yes, scrollbars=yes, toolbar=no, width=400, height=400);
event_popupWin.opener = self;
}
</script>
<body>
<h1>Select a Night Out</h1>
<table width="200">
<tr align="center">
<td bgcolor="#999999" style="color:#FFFFFF">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%" align="left"> Previous</td>
<td width="50%" align="right">Next </td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center">
<table width="100%" border="0" cellpadding="2" cellspacing="2">
<tr align="center">
<td colspan="7" bgcolor="#999999" style="color:#FFFFFF"><strong><?php echo $monthNames[$sMonth - 1] . ' ' . $sYear; ?></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
$timestamp = mktime(0, 0, 0, $sMonth, 1, $sYear);
if ($cDay != '') {
$selectedDate = mktime(0, 0, 0, $cMonth, $cDay, $cYear);
} else {
$selectedDate = 0;
}
$maxday = date("t", $timestamp);
$thismonth = getdate($timestamp);
$startday = $thismonth['wday'];
$today = date('j');
$currentmonth = date('n');
for ($i = 0; $i < ($maxday + $startday); $i++) {
if (($i % 7) == 0) {
echo "<tr>";
}
if ($i < $startday) {
echo ("<td class='cell cell_txt'> </td>");
} else {
$testDate = mktime(0, 0, 0, $sMonth, $i - $startday + 1, $sYear);
if ($testDate < $selectedDate) {
echo ("<td class='cell_today cell_txt'>" . ($i - $startday + 1) . "</td>");
} else {
echo ("<td class='cell cell_txt'>" . ($i - $startday + 1) . "</td>");
}
}
if (($i % 7) == 6) {
echo "</tr>\n";
}
}
?>
</table>
</td>
</tr>
</table>
</body>
</html>
<!-----use it will surely work -----!>
<?php
$monthNames = Array("January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December");
?>
<?php
if (!isset($_REQUEST["day"])) $_REQUEST["day"] = date("d");
if (!isset($_REQUEST["month"])) $_REQUEST["month"] = date("n");
if (!isset($_REQUEST["year"])) $_REQUEST["year"] = date("Y");
?>
<?php
$cDay = $_REQUEST["day"];
$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;
}
?>
<div id="calendar_div" name="calendar_div">
<div class="table-responsive">
<table width="93%" style="border: none">
<tr>
<td> <a
href="<?php echo $_SERVER["PHP_SELF"] . "?month=" . $prev_month . "&year=" . $prev_year; ?>"
style="color:#FFFFFF"> < </a>
<a href="<?php echo $_SERVER["PHP_SELF"] . "?month=" . $next_month . "&year=" . $next_year; ?>"
style="color:#FFFFFF"> > </a></td>
<td style="float: right">
<h3 style="color: #ffffff;"> <?php echo $monthNames[$cMonth - 1] . ' ' . $cYear; ?></strong></h3>
</td>
</tr>
</table>
<table class="table">
<tr>
<td style="color:#FFFFFF;border: none"><strong>Sun</strong></td>
<td style="color:#FFFFFF;border: none"><strong>Mon</strong></td>
<td style="color:#FFFFFF;border: none"><strong>Tue</strong></td>
<td style="color:#FFFFFF;border: none"><strong>Wed</strong></td>
<td style="color:#FFFFFF;border: none"><strong>Thr</strong></td>
<td style="color:#FFFFFF;border: none"><strong>Fri</strong></td>
<td style="color:#FFFFFF;border: none"><strong>Sat</strong></td>
</tr>
<?php
$timestamp = mktime(0, 0, 0, $cMonth, 1, $cYear);
$maxday = date("t", $timestamp);
$thismonth = getdate($timestamp);
$currentmonth = date('n');
$startday = $thismonth['wday'];
for ($i = 0; $i < ($maxday + $startday); $i++) {
if (($i % 7) == 0) echo "<tr>\n";
if ($i < $startday) echo "<td style='border: none'></td>\n";
elseif(($i - $startday + 1) == $cDay && $currentmonth == $cMonth ){
echo "<td style='background-color: #cccccc'>". ($i - $startday + 1) ."</td>";}
else{
echo "<td style='color: #ffffff; border: none'>". ($i - $startday + 1) ."</td></a>";}
if(($i % 7) == 6 ) echo "</tr>";
}
?>
</table>

to display calendar using javascript and php

im new to this PHP im trying to develop a code such that when i click launch calendar it ll display calendar and when i choose the date it ll display in the text field but my problem is when i clik launch calendar its not showing feburay month.. i dont no wt the error in my code can anyone pls help me out in this.. thanks in advance.. here is my code what i have developed...
<?php
$day = $_GET["day"];
$month = $_GET["month"];
$year = $_GET["year"];
$sel = $_GET["sel"];
$what = $_GET["what"];
$field = $_GET["field"];
$form = $_GET["form"];
if($day == "") $day = date("j");
if($month == "") $month = date("m");
if($year == "") $year = date("Y");
$currentTimeStamp = strtotime("$year-$month-$day");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t", $currentTimeStamp);
$counter = 0;
?>
<html>
<head>
<title>MyCalendar</title>
<link rel="stylesheet" type="text/css" href="calendar.css">
<script language="javascript">
function goLastMonth(month,year,form,field)
{
if(month == 1)
{
--year;
month = 13;
}
document.location.href = 'calendar.php?month='+(month-1)+'&year='+year+'&form='+form+'&field='+field;
}
function goNextMonth(month,year,form,field)
{
if(month == 12)
{
++year;
month = 0;
}
document.location.href = 'calendar.php?month='+(month+1)+'&year='+year+'&form='+form+'&field='+field;
}
function sendToForm(val,field,form)
{
eval("opener.document." + form + "." + field + ".value='" + val + "'");
window.close();
}
</script>
</head>
<body style="margin:0px 0px 0px 0px" class="body">
<table width='175' border='0' cellspacing='0' cellpadding='0' class="body">
<tr>
<td width='25' colspan='1'>
<input type='button' class='button' value=' < ' onClick='<?php echo "goLastMonth($month,$year,\"$form\",\"$field\")"; ?>'>
</td>
<td width='125' align="center" colspan='5'>
<span class='title'><?php echo $monthName . " " . $year; ?></span><br>
</td>
<td width='25' colspan='1' align='right'>
<input type='button' class='button' value=' > ' onClick='<?php echo "goNextMonth($month,$year,\"$form\",\"$field\")"; ?>'>
</td>
</tr>
<tr>
<td class='head' align="center" width='25'>S</td>
<td class='head' align="center" width='25'>M</td>
<td class='head' align="center" width='25'>T</td>
<td class='head' align="center" width='25'>W</td>
<td class='head' align="center" width='25'>T</td>
<td class='head' align="center" width='25'>F</td>
<td class='head' align="center" width='25'>S</td>
</tr>
<tr>
<?php
for($i = 1; $i < $numDays+1; $i++, $counter++)
{
$timeStamp = strtotime("$year-$month-$i");
if($i == 1)
{
$firstDay = date("w", $timeStamp);
for($j = 0; $j < $firstDay; $j++, $counter++)
echo "<td> </td>";
}
if($counter % 7 == 0)
echo "</tr><tr>";
if(date("w", $timeStamp) == 0)
$class = "class='weekend'";
else
if($i == date("d") && $month == date("m") && $year == date("Y"))
$class = "class='today'";
else
$class = "class='normal'";
echo "<td class='tr' bgcolor='#ffffff' align='center' width='25'><a class='buttonbar' href='#' onclick=\"sendToForm('".sprintf("%02d/%02d/%04d", $month, $i, $year)."','$field','$form');\"><font $class>$i</font></a></td>";
}
?>
</tr>
</table>
</body>
</html>
This is not a fix to your script but may be a fix to your problem. Isn't it easier to just use a pre made complete javascript calendar library?
like:
Jscalendar Demo
Or
Jquery Ui Datepicker + Demo
as the calendar is going to run on the client side this task can be done using only client side code. using javascript and pre made library like Jquery - as #RJD22 pointed out.
i think that in general when you can get a task done in client side insted of using server side it is better because server resources are limited.

Categories