PHP calendar showing dates on the right day of the week - php

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

Related

How to calculate data from database in current month from first date of month until current date in month

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>

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

PHP Calendar table

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!

Finishing Calendar table <tr>, <td> with php code [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
When in Notepad++ anything in "" inside php in block will not display and finish the table. So the echo "<tr>"; turns grey and does not finish the table. Any suggestions?
<table border='1'>
<tr>
<td><input type='button' value='<' name='previousebutton' onclick="goLastMonth(<?php echo $month.",".$year?>)"></td>
<td colspan='5'> <?php echo $monthName.", ".$year ?> </td>
<td><input type='button' value='>' name='nextbutton'onclick="goNextMonth(<?php echo $month.",".$year?>)" ></td>
</tr>
<tr>
<td width='50px'>Sun</td>
<td width='50px'>Mon</td>
<td width='50px'>Tue</td>
<td width='50px'>Wed</td>
<td width='50px'>Thu</td>
<td width='50px'>Fri</td>
<td width='50px'>Sat</td>
</tr>
<?php
echo "<tr>";
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>$nbsp;</td>";
}
}
if($counter % 7 == 0) {
echo "</tr><tr>";
}
}
echo "</tr>";
?>
</table>
Modified some of your code:
<?php
if(isset($_GET['day'])){ $day = $_GET['day']; }else{ $day = date("j"); }
if(isset($_GET['month'])){ $month = $_GET['month']; }else{ $month = date("n"); }
if(isset($_GET['year'])){ $year = $_GET['year']; }else{ $year = date("Y"); }
//calender variable
$currentTimeStamp = strtotime("$year-$month-$day");
$monthName = date("F", $currentTimeStamp);
$numDays = date("t" , $currentTimeStamp);
$counter = 1;
$day = date('d', $currentTimeStamp);
$firstDay = date('w', strtotime("{$year}-{$month}-01"));
?>
<table border='1'>
<tr>
<td><input type='button' value='<' name='previousebutton' onclick="goLastMonth(<?php echo $month.",".$year?>)"></td>
<td colspan='5'> <?php echo $monthName.", ".$year ?> </td>
<td><input type='button' value='>' name='nextbutton'onclick="goNextMonth(<?php echo $month.",".$year?>)" ></td>
</tr>
<tr>
<td width='50px'>Sun</td>
<td width='50px'>Mon</td>
<td width='50px'>Tue</td>
<td width='50px'>Wed</td>
<td width='50px'>Thu</td>
<td width='50px'>Fri</td>
<td width='50px'>Sat</td>
</tr>
<tr>
<?php for($i = 0; $i < $firstDay; $i++): ?>
<td></td>
<?php endfor; ?>
<?php for($i = 1; $i <= $numDays; $i++): ?>
<?php if($day == $i): ?>
<td><strong><?php echo $i ?></strong></td>
<?php else: ?>
<td><?php echo $i ?></td>
<?php endif; ?>
<?php if(($i + $firstDay) % 7 == 0): ?>
</tr><tr>
<?php endif; ?>
<?php endfor; ?>
<?php for($i = 0; ($i + $firstDay + $numDays) % 7 != 0; $i++): ?>
<td></td>
<?php endfor; ?>
</tr>
</table>
You can try to use the alternate control structure syntax for this, although (in my opinion) it can make some code harder to read:
<?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++): ?>
<td>$nbsp;</td>
<?php endfor; ?>
}
if($counter % 7 == 0): ?>
</tr><tr>
<?php endif; ?>
}
?>

Table headings not lining it sometimes when there is data in them

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

Categories