I have a calendar I created for a bottling schedule. You can drag and drop items from one data to another and it updates the database. This part works fine.
http://thereal805productions.com/calendar/calendar.php
However, for some reason, the headers "sometimes" don't line up properly when they have data in them. They will be down somewhere between .5 to 1 table cell below where they should be. I'm sure it's just something bone headed, but I can't figure it out.
Any help is appreciated.
The what I think is offending part of the code is here:
while ( $day_num <= $days_in_month ) {
$matched = false;
?>
<td> <!-- this should be a table inside each date -->
<table>
<th> <div id = "<?php echo $myDate ?>" class = "droppable ui-widget-header" > <?php echo $day_num ?> </div> </th>
<?php
$result = $conn->query($sql);
foreach ($result as $row) {
// echo "row = " . $row['date'] . " and myDate = " . $myDate;
if ($row['botdate'] == $myDate) { ?>
<tr><td><div id = '<?php echo $row['id'] ?>' class="draggable ui-widget-content "> <?php echo $row['productId']; ?> </div></td></tr>
<?php $matched = true; } else if ($matched == false) { ?>
<tr><td> </td></tr>
<?php }
} ?> <!-- ends the foreach -->
</table>
</td> <!-- ends the table inside each date -->
<?php
$day_num++;
$day_count++;
$myDate = strtotime("+1 day", strtotime($myDate));
$myDate = date("Y-m-d", $myDate);
//Make sure we start a new row every week
if ($day_count > 7) {
echo "</tr><tr>";
$day_count = 1;
}
} // ends the while loop
The complete code is probably not needed, but I included it for completeness sake.
<?php
require_once('../includes/connection.inc.php');
//This gets today's date
$date =time () ;
//This puts the day, month, and year in seperate variables
$day = date('d', $date) ;
$month = date('m', $date) ;
$year = date('Y', $date) ;
//Here we generate the first day of the month
$first_day = mktime(0,0,0,$month, 1, $year) ;
//This gets us the month name
$title = date('F', $first_day) ;
//Here we find out what day of the week the first day of the month falls on
$day_of_week = date('D', $first_day) ;
//Once we know what day of the week it falls on, we know how many blank days occure before it. If the first day of the week is a Sunday then it would be zero
switch($day_of_week){
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;
case "Sat": $blank = 6; break;
}
//We then determine how many days are in the current month
$days_in_month = cal_days_in_month(0, $month, $year) ;
//Here we start building the table heads
?>
<table border=1 id = 'calendar'>
<tr>
<th colspan=7> <?php echo $title." ". $year; ?></th>
</tr>
<tr>
<th class = 'weekday' width=42>S</th>
<th class = 'weekday' width=42>M</th>
<th class = 'weekday' width=42>T</th>
<th class = 'weekday' width=42>W</th>
<th class = 'weekday' width=42>T</th>
<th class = 'weekday' width=42>F</th>
<th class = 'weekday' width=42>S</th>
</tr>
<?php
//This counts the days in the week, up to 7
$day_count = 1;
echo "<tr>";
//first we take care of those blank days
while ( $blank > 0 ) {
echo "<td></td>";
$blank = $blank-1;
$day_count++;
}
//sets the first day of the month to 1
$day_num = 1;
$myDate = '2014-05-01';
$conn = dbConnect();
$sql = ('SELECT * FROM bottling');
//count up the days, untill we've done all of them in the month
while ( $day_num <= $days_in_month ) {
$matched = false;
?>
<td> <!-- this should be a table inside each date -->
<table>
<th> <div id = "<?php echo $myDate ?>" class = "droppable ui-widget-header" > <?php echo $day_num ?> </div> </th>
<?php
$result = $conn->query($sql);
foreach ($result as $row) {
// echo "row = " . $row['date'] . " and myDate = " . $myDate;
if ($row['botdate'] == $myDate) { ?>
<tr><td><div id = '<?php echo $row['id'] ?>' class="draggable ui-widget-content "> <?php echo $row['productId']; ?> </div></td></tr>
<?php $matched = true; } else if ($matched == false) { ?>
<tr><td> </td></tr>
<?php }
} ?> <!-- ends the foreach -->
</table>
</td> <!-- ends the table inside each date -->
<?php
$day_num++;
$day_count++;
$myDate = strtotime("+1 day", strtotime($myDate));
$myDate = date("Y-m-d", $myDate);
//Make sure we start a new row every week
if ($day_count > 7) {
echo "</tr><tr>";
$day_count = 1;
}
} // ends the while loop
//Finaly we finish out the table with some blank details if needed
while ( $day_count >1 && $day_count <=7 )
{
echo "<td> </td>";
$day_count++;
}
echo "</tr></table>";
Figured it out, I should be using valign = "top"
so
This makes the table inside the td align to the top
Related
The problem for this code is it calculate all data from all month that has in the database.. It not from this current month from first date of this month until current date of this month...Can someone help me...
<tbody>
<?php
$FullTotal = 0;
while($rowF = $resultX -> fetch_assoc()){// registerfighter
$totalPlus = 0;
//table start
?>
<tr>
<td><?php echo $rowF["UserName"] ?></td>
<?php
$resultY-> data_seek(0);
while($rowY = $resultY -> fetch_assoc()){ // by addplan
$resultR -> data_seek(0);
while($rowR = $resultR -> fetch_assoc()){ // by record
if ($first_date <= $rowR["SubmitDate"] && $last_date <= $rowR["SubmitDate"]){
// this the problems...it not work
if( ($rowF["UserID"]===$rowR["UserIDD"]) and
(($rowR["No_Plan"]) === '1' and
($rowY["No_Plan"] === $rowR['No_Plan']))) {
$total = ($rowR["UserInputNEW"] + $rowR["UserInputNMP"]);
$totalPlus = $totalPlus + $total;
$FullTotal = $FullTotal + $total;
}
}
}
}
?>
<td><?php echo $totalPlus; ?></td>
</tr>
<?php
//table end
} ?>
<td colspan="1"> Total </td>
<td colspan="1"> <?php echo $FullTotal?></td>
</tbody>
Hi Im trying to make a calendar table that displays the months in a year and days in a week.
I am able to make one for this month(Nov 2016). Now I want to loop it through out the year and for coming years.
Can someone help me?
<?php
/* Set the default timezone */
date_default_timezone_set("Asia/Hong_Kong");
/* Set the date */
$date = strtotime(date("Y-m-d"));
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
// $nextyear = strtotime('+1 month', $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 < 32; $i++) {
$weekDays[] = strftime('%a', $timestamp);
$timestamp = strtotime('+1 day', $timestamp);
}
$blank = date('w', strtotime("{$year}-{$month}-01"));
?>
<table class='table table-bordered' style="table-layout: fixed;">
<tr>
<th colspan="32" 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) % 32 == 0): ?>
</tr><tr>
<?php endif; ?>
<?php endfor; ?>
<?php for($i = 0; ($i + $blank + $daysInMonth) % 32 != 0; $i++): ?>
<td></td>
<?php endfor; ?>
</tr>
</table>
As said in the comments above you can use two loops nested inside each other, one for the years, one for the months. That way you can execute your already given code based on different dates but the current one.
Have a look at this approach. It basically does what you are looking for, but you need to do some fine tuning for the layout:
<?php
define('NUMBER_OF_COLUMNS', 37);
function renderCalenderMonth($date) {
$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 < NUMBER_OF_COLUMNS; $i++) {
$weekDays[] = strftime('%a', $timestamp);
$timestamp = strtotime('+1 day', $timestamp);
}
$blank = date('w', strtotime("{$year}-{$month}-01"));
?>
<table class='table table-bordered' style="table-layout: fixed;">
<tr>
<th colspan="<?php echo NUMBER_OF_COLUMNS?>" 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) % NUMBER_OF_COLUMNS == 0): ?>
</tr><tr>
<?php endif; ?>
<?php endfor; ?>
<?php for($i = 0; ($i + $blank + $daysInMonth) % NUMBER_OF_COLUMNS != 0; $i++): ?>
<td></td>
<?php endfor; ?>
</tr>
</table>
<?php
}
// ===========================
/* Set the default timezone */
date_default_timezone_set("Asia/Hong_Kong");
for ($iterateYear=2016; $iterateYear<2018; $iterateYear++) {
for ($iterateMonth=1; $iterateMonth<=12; $iterateMonth++) {
/* Set the date */
$date = strtotime(sprintf('%s-%s-01', $iterateYear, $iterateMonth));
renderCalenderMonth($date);
}
}
Laravel 5.3 version of calendar code:
public function index()
{
$users = DB::table('users')->where('roles_id', '2')->get();
date_default_timezone_set("Asia/Hong_Kong");
for ($iterateYear = 2017; $iterateYear < 2018; $iterateYear++) {
for ($iterateMonth = 1; $iterateMonth <= 12; $iterateMonth++) {
/* Set the date */
$date = strtotime(sprintf('%s-%s-01', $iterateYear, $iterateMonth));
return $this->renderCalenderMonth($date);
}
}
return view('transaction.index', [
'users' => $users
]);
}
public function renderCalenderMonth($date)
{
// $date = strtotime(date("Y-m-d"));
$NUMBER_OF_COLUMNS = 37;
$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 = [];
for ($i = 0; $i < $NUMBER_OF_COLUMNS; $i++) {
$weekDays[] = strftime('%a', $timestamp);
$timestamp = strtotime('+1 day', $timestamp);
}
$blank = date('w', strtotime("{$year}-{$month}-01"));
$divid = $title . $year;
return view('transaction.index', [
'date' => $date,
'day' => $day,
'month' => $month,
'year' => $year,
'firstDay' => $firstDay,
'title' => $title,
'dayOfWeek' => $dayOfWeek,
'daysInMonth' => $daysInMonth,
'timestamp' => $timestamp,
'weekDays' => $weekDays,
'blank' => $blank,
'divid' => $divid,
'NUMBER_OF_COLUMNS' => $NUMBER_OF_COLUMNS
]);
}
I'm only getting this. Thanks for the help!
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
Hii i am working on portal which requires calendar from custom dates like from 14 Jul to 14 Aug.I have made a demo which starts properly i.e.if 14th july starts from Thursday but not ending at sunday which is on 14 aug. Take a look what i have done
function getCalender($year = '',$month = '')
{
$startDate = "2016-07-03";
$endDate = "2016-07-31";
$dateYear = ($year != '')?$year:date("Y");
$dateMonth = ($month != '')?$month:date("m");
$date = $dateYear.'-'.$dateMonth.'-01';
$currentMonthFirstDay = date("N",strtotime($date));
$startDateFrom = date("N",strtotime($startDate));
$endDateFrom = date("N",strtotime($endDate));
$timestamp = strtotime($startDate);
$dayCount = date("d", $timestamp);
$timestamp1 = strtotime($endDate);
$dayCountend = date("d", $timestamp1);
$totalDaysOfMonth = cal_days_in_month(CAL_GREGORIAN,$dateMonth,$dateYear);
$totalDaysOfMonthDisplay = ($currentMonthFirstDay == 7)?($totalDaysOfMonth):($totalDaysOfMonth + $currentMonthFirstDay);
//$boxDisplay = ($totalDaysOfMonthDisplay <= 35)?35:42;
//$boxDisplay = 29
?>
<div id="calender_section">
<h2>
<!--<<-->
<select name="month_dropdown" class="month_dropdown dropdown"><?php echo getAllMonths($dateMonth); ?></select>
<select name="year_dropdown" class="year_dropdown dropdown"><?php echo getYearList($dateYear); ?></select>
<!-->>-->
</h2>
<div id="event_list" class="none"></div>
<!--For Add Event-->
<div id="event_add" class="none">
<p>Add Event on <span id="eventDateView"></span></p>
<p><b>Event Title: </b><input type="text" id="eventTitle" value=""/></p>
<input type="hidden" id="eventDate" value=""/>
<input type="button" id="addEventBtn" value="Add"/>
</div>
<div id="calender_section_top">
<ul>
<li>Mon</li>
<li>Tue</li>
<li>Wed</li>
<li>Thu</li>
<li>Fri</li>
<li>Sat</li>
<li>Sun</li>
</ul>
</div>
<div id="calender_section_bot">
<ul>
<?php
for($cb=1;$cb<=$dayCountend;$cb++){
//echo $cb;
if(($cb >= $startDateFrom) && $cb<=$dayCountend)
{
/*if(($cb >= $currentMonthFirstDay+1 || $currentMonthFirstDay == 7) && $cb <= ($totalDaysOfMonthDisplay)){*/
//Current date
$currentDate = $dateYear.'-'.$dateMonth.'-'.$dayCount;
$currentdate = date("D M j",strtotime($currentDate));
$eventNum = 0;
//Include db configuration file
include 'dbConfig.php';
//Get number of events based on the current date
$result = $db->query("SELECT title FROM events WHERE date = '".$currentDate."' AND status = 1");
$eventNum = $result->num_rows;
//Define date cell color
if(strtotime($currentDate) == strtotime(date("Y-m-d"))){
echo '<li date="'.$currentDate.'" class="grey date_cell">';
}elseif($eventNum > 0){
echo '<li date="'.$currentDate.'" class="light_sky date_cell">';
}else{
echo '<li date="'.$currentDate.'" class="date_cell">';
}
//Date cell
echo '<span>';
echo $cb;
echo '</span>';
//Hover event popup
/*echo '<div id="date_popup_'.$currentDate.'" class="date_popup_wrap none">';
echo '<div class="date_window">';
echo '<div class="popup_event">Events ('.$eventNum.')</div>';
echo ($eventNum > 0)?'view events<br/>':'';
//For Add Event
echo 'add event';
echo '</div></div>';
echo '</li>';*/
$dayCount++;
?>
<?php }else{ ?>
<li><span> </span></li>
<?php } } ?>
</ul>
</div>
</div>
<?php
}
?>
I tried for your code and i found one logic i took start date and end date from the user and made the array of the dates which are present between two.
Then calendar should start from for ex. 3 Jul 2016 the day is Sunday for that i inserted - upto first date. Check below what i have done
function getCalender($year = '',$month = '',$startDate = '',$endDate = '')
{
$finalArray = array();
$startDate = "2016-07-03";
$endDate = "2016-08-31";
$date1 = createDateRangeArray('2016-07-03 ','2016-08-31');
//$date = $dateYear.'-'.$dateMonth.'-01';
$startDateFrom = date("N",strtotime($startDate));
$endDateFrom = date("N",strtotime($endDate));
//Take the day from the date
$timestamp = strtotime($startDate);
$dayCount = date("d", $timestamp);
//Take the month from the date
$timestampend = strtotime($endDate);
$dayCountend = date("d", $timestampend);
//Take the month from the date
$timestampmonth = strtotime($startDate);
$monthstart = date("m", $timestampmonth);
//Take the month from the date
$timestampmonthend = strtotime($endDate);
$monthend = date("m", $timestampmonthend);
//Take the year from the start date
$yearstart = strtotime($startDate);
$yearofStart = date("y", $yearstart);
//Take the year from the end date
$yearend = strtotime($endDate);
$yearofEnd = date("y", $yearend);
//cal difference between start date and month
$differenceStartDate = $dayCount - 1;
//cal difference between end date and month
$totalDaysOfMonthEnd = cal_days_in_month(CAL_GREGORIAN,$monthend,$yearofEnd);
$totalDaysOfMonthStart = cal_days_in_month(CAL_GREGORIAN,$monthstart,$yearofEnd);
if($monthstart == $monthend)
{
echo "Show one month calendar";
}
else
{
$diffmonth = $monthend-$monthstart;
}
$differenceStartDate = $dayCountend - $totalDaysOfMonth;
for($i=1;$i<$startDateFrom;$i++)
{
$date[] = '-';
}
array_push($date,$date1);
for($j=0;$j<count($date);$j++)
{
if(strlen($date[$j])==1)
{
array_push($finalArray,$date[$j]);
}
else
{
for($k=0;$k<count($date[$j]);$k++)
{
array_push($finalArray,$date[$j][$k]);
}
}
}
print_r($finalArray);
$timestamp1 = strtotime($endDate);
$dayCountend = date("d", $timestamp1);
$now = strtotime($startDate);
$your_date = strtotime($endDate);
$datediff = $your_date - $now;
$diff= floor($datediff/(60*60*24));
$totalDaysOfMonth = cal_days_in_month(CAL_GREGORIAN,$dateMonth,$dateYear);
$totalDaysOfMonthDisplay = ($currentMonthFirstDay == 7)?($totalDaysOfMonth):($totalDaysOfMonth + $currentMonthFirstDay);
//$boxDisplay = ($totalDaysOfMonthDisplay <= 35)?35:42;
$boxDisplay = $diff;
?>
<div id="calender_section">
<h2>
<!--<<-->
<!-- <select name="month_dropdown" class="month_dropdown dropdown"><?php echo getAllMonths($dateMonth); ?></select>
<select name="year_dropdown" class="year_dropdown dropdown"><?php echo getYearList($dateYear); ?></select>-->
<!-->>-->
</h2>
<div id="event_list" class="none"></div>
<!--For Add Event-->
<div id="event_add" class="none">
<p>Add Event on <span id="eventDateView"></span></p>
<p><b>Event Title: </b><input type="text" id="eventTitle" value=""/></p>
<input type="hidden" id="eventDate" value=""/>
<input type="button" id="addEventBtn" value="Add"/>
</div>
<div id="calender_section_top">
<ul>
<li>Mon</li>
<li>Tue</li>
<li>Wed</li>
<li>Thu</li>
<li>Fri</li>
<li>Sat</li>
<li>Sun</li>
</ul>
</div>
<div id="calender_section_bot">
<ul>
<?php
for($cb=0;$cb<count($finalArray);$cb++)
{
//echo $cb;
$date = $finalArray[$cb];
$month = strtotime($date);
$month = date("m", $month);
if(($cb >= $startDateFrom -1))
{
/*if(($cb >= $currentMonthFirstDay+1 || $currentMonthFirstDay == 7) && $cb <= ($totalDaysOfMonthDisplay)){*/
//Current date
$currentDate = $finalArray[$cb];
//$currentdate = date("D M j",strtotime($currentDate));
$eventNum = 0;
//Include db configuration file
//Define date cell color
if(strtotime($currentDate) == strtotime(date("Y-m-d"))){
echo '<li date="'.$currentDate.'" class="grey date_cell">';
echo '<select name="select"></select>
<option value="0"></option>';
}elseif($eventNum > 0){
echo '<li date="'.$currentDate.'" class="light_sky date_cell">';
echo '<select name="select"></select>
<option value="0"></option>';
}else{
echo '<li date="'.$currentDate.'" class="date_cell">';
echo '<select name="select"></select>
<option value="0"></option>';
}
//Date cell
echo '<span>';
echo $currentDate;
echo '</span>';
$dayCount++;
?>
<?php }else{ ?>
<li><span> </span></li>
<?php } } ?>
</ul>
</div>
</div>
<?php
}
function createDateRangeArray($strDateFrom,$strDateTo)
{
// takes two dates formatted as YYYY-MM-DD and creates an
// inclusive array of the dates between the from and to dates.
// could test validity of dates here but I'm already doing
// that in the main script
$aryRange=array();
$iDateFrom=mktime(1,0,0,substr($strDateFrom,5,2), substr($strDateFrom,8,2),substr($strDateFrom,0,4));
$iDateTo=mktime(1,0,0,substr($strDateTo,5,2), substr($strDateTo,8,2),substr($strDateTo,0,4));
if ($iDateTo>=$iDateFrom)
{
array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry
while ($iDateFrom<$iDateTo)
{
$iDateFrom+=86400; // add 24 hours
array_push($aryRange,date('Y-m-d',$iDateFrom));
}
}
return $aryRange;
}
?>
Take a look at a code and you will understand that easily
The answer given by #BeDeveloper is fine.I made something like below which automatically disable all the other dates for ex.if date 3 it will disable 1 and 2
<style>
/* calendar */
h1 { font-family: calibri; font-size: 20px; color: #6d6d6d}
table.calendar { border-left:1px solid #999; }
tr.calendar-row { }
td.calendar-day { min-height:80px; font-size:11px; position:relative; } * html div.calendar-day { height:80px; }
td.calendar-day:hover { background:#eceff5; }
td.calendar-day-np { background:#eee; min-height:80px; } * html div.calendar-day-np { height:80px; }
td.calendar-day-head { background:#ccc; font-weight:bold; text-align:center; width:120px; padding:5px; border-bottom:1px solid #999; border-top:1px solid #999; border-right:1px solid #999; }
div.day-number { background:#999; padding:5px; color:#fff; font-weight:bold; float:right; margin:-5px -5px 0 0; width:20px; text-align:center; }
/* shared */
td.calendar-day, td.calendar-day-np { width:120px; padding:5px; border-bottom:1px solid #999; border-right:1px solid #999; }
</style>
<?php
/* draws a calendar */
function draw_calendar($month,$year,$startDate,$endDate){
$startDate = date('Y-m-d',$startDate);
$endDate = date('Y-m-d',$endDate);
$timestamp = strtotime($startDate);
$dayCountstart = date("d", $timestamp);
$timestamp = strtotime($endDate);
$dayCountend = date("d", $timestamp);
$timestampmonth = strtotime($startDate);
$monthstart = date("m", $timestampmonth);
$timestampmonthend = strtotime($endDate);
$monthend = date("m", $timestampmonthend);
$yearend = strtotime($endDate);
$yearofEnd = date("y", $yearend);
$dayMinus = $dayCountstart - 01;
$totalDaysOfMonthEnd = cal_days_in_month(CAL_GREGORIAN,$monthend,$yearofEnd);
$dayEndMinus = $totalDaysOfMonthEnd - $dayCountend;
/* draw table */
$calendar = '<table cellpadding="0" cellspacing="0" class="calendar">';
/* table headings */
$headings = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$calendar.= '<tr class="calendar-row"><td class="calendar-day-head">'.implode('</td><td class="calendar-day-head">',$headings).'</td></tr>';
/* days and weeks vars now ... */
$running_day = date('w',mktime(0,0,0,$month,1,$year));
$days_in_month = date('t',mktime(0,0,0,$month,1,$year));
$days_in_this_week = 1;
$day_counter = 0;
$dates_array = array();
/* row for week one */
$calendar.= '<tr class="calendar-row">';
/* print "blank" days until the first of the current week */
for($x = 0; $x < $running_day; $x++):
$calendar.= '<td class="calendar-day-np"> </td>';
$days_in_this_week++;
endfor;
/* keep going with days.... */
for($list_day = 1; $list_day <= $days_in_month; $list_day++):
$calendar.= '<td class="calendar-day">';
/* add in the day number */
if($monthstart == $month)
{
if($list_day <= $dayMinus)
{
$calendar.= '<div class="day-number123">'.$list_day.'</div>';
}
else
{
$calendar.= '<div class="day-number">'.$list_day.'</div>';
}
}
else
{
if($monthend == $month)
{
if($list_day <= $dayCountend)
{
$calendar.= '<div class="day-number">'.$list_day.'</div>';
}
else
{
$calendar.= '<div class="day-number123">'.$list_day.'</div>';
}
}
else
{
$calendar.= '<div class="day-number">'.$list_day.'</div>';
}
}
/** QUERY THE DATABASE FOR AN ENTRY FOR THIS DAY !! IF MATCHES FOUND, PRINT THEM !! **/
$calendar.= str_repeat('<p> </p>',2);
$calendar.= '</td>';
if($running_day == 6):
$calendar.= '</tr>';
if(($day_counter+1) != $days_in_month):
$calendar.= '<tr class="calendar-row">';
endif;
$running_day = -1;
$days_in_this_week = 0;
endif;
$days_in_this_week++; $running_day++; $day_counter++;
endfor;
/* finish the rest of the days in the week */
/* if($days_in_this_week < 8):
for($x = 1; $x <= (8 - $days_in_this_week); $x++):
$calendar.= '<td class="calendar-day-np"> </td>';
endfor;
endif;*/
/* final row */
$calendar.= '</tr>';
/* end the table */
$calendar.= '</table>';
/* all done, return result */
return $calendar;
}
/* sample usages */
/*echo '<h2>July 2009</h2>';
echo draw_calendar(7,2009);
echo '<h2>August 2009</h2>';
echo draw_calendar(8,2009);
*/
$start = $startDate= $month = strtotime('2016-02-02');
$end = $endDate = strtotime('2016-03-03');
while($month < $end)
{
//$a = date('m Y', $month), PHP_EOL;
$startDate = $start;
$endDate = $end;
$newMonthInRange = date('m', $month);
$newYearInRange = date('Y', $month);
$newMonthInRangeTitle = date('F Y', $month);
$month = strtotime("+1 month", $month);
echo "<h1>".$newMonthInRangeTitle."</h1>";
echo draw_calendar($newMonthInRange,$newYearInRange,$startDate,$endDate);
}
?>
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.