Echo date interval in a PHP calendar - php

I am creating a calendar and I am in a dire need of a solution. There are records of information in the database and the code reads these records. Two columns hold a date from and a date to. I have to output these dates that appear in the database in different colors. I have tried many times. And the problem is that the code outputs either the last record that it can fetch in the database or all the date periods however these periods get duplicated based on how many rows are there in the database.Here is the output I get
<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
$dateYear = ($year != '') ? $year : date("Y");
$dateMonth = ($month != '') ? $month : date("m");
$date = $dateYear . '-' . $dateMonth . '-01';
$currentMonthFirstDay = date("N", strtotime($date));
$totalDaysOfMonth = cal_days_in_month(CAL_GREGORIAN, $dateMonth, $dateYear);
$totalDaysOfMonthDisplay = ($currentMonthFirstDay == 7) ? ($totalDaysOfMonth) : ($totalDaysOfMonth + $currentMonthFirstDay);
$boxDisplay = ($totalDaysOfMonthDisplay <= 35) ? 35 : 42;
$dayCount = 1;
for ($cb = 1; $cb <= $boxDisplay; $cb++) {
if (($cb >= $currentMonthFirstDay || $currentMonthFirstDay == 7) && $cb <= ($totalDaysOfMonthDisplay - 1)) {
$duh = array();
array_push($duh, $dayCount);
// Current date
$currentDate = $dateYear . '-' . $dateMonth . '-' . $dayCount;
$currentDate = strtotime($currentDate);
$eventNum = 0;
$sql = ("SELECT * FROM booking_2 GROUP BY date_from ORDER BY COUNT(book2_id) DESC");
$result = $this->connect()->query($sql);
$eventNum = $result->num_rows;
if ($eventNum > 0) {
while ($row = $result->fetch_assoc()) {
$date_from = $row['date_from'];
$date_to = $row['date_to'];
$time_from = strtotime($date_from);
$time_fromm = idate('d', $time_from);
$time_to = strtotime($date_to);
$time_too = idate('d', $time_to);
if ($currentDate >= $time_from && $currentDate <= $time_to) {
for ($i = $time_from; $i <= $time_to; $i++) {
$cope = array();
array_push($cope, $dayCount);
}
echo '<li id="' . $row['book2_id'] . '" style="background-color:#FF3232 !important;" date="' . date("Y-m-d H:i:s", $currentDate) . '" class="date_cell"><span>' . implode($cope) . '</span>';
}
}
if (!($currentDate >= $time_from && $currentDate <= $time_to)) {
for ($g = $time_from; $g <= $time_to; $g++) {
$duh = array();
array_push($duh, $dayCount);
}
echo '<li date="' . date("Y-m-d H:i:s", $currentDate) . '" class="date_cell"><span>' . implode($duh) . '</span>';
}
}
else {
echo '<li date="' . date("Y-m-d H:i:s", $currentDate) . '" class="date_cell"><span>' . implode($duh) . '</span>';
}
echo '</li>';
$dayCount++;
?>
<?php
}
else { ?>
<li><span> </span></li>
<?php
}
} ?>
</ul>
</div>
</div>
sorry for TL;DR. But really need help with this. I have tried all the ways I know so if you have a solution can you please be sure that this will work? I believe this is a hard problem to find a solution to.

Related

How to display time_ago in another function

This function is to display what inside my table but i can't figure out how to dislay time_ago function where it should display seconds or hours or weeks just want to display seconds hour or week instead of month day and year but i get an error Call to undefined function time_ago() i don't know what i did wrong but i just can figure it out what went wrong with my code. got error on line 505 which is this ---> ' . time_ago($row['lastupdate']) . '
public static function getTicketByUserID(){
$mysqli = dbConnect();
$sql = "select
t.id as ticketid,
t.clientid,
t.comments,
t.assigneduser,
t.subject,
t.user,
t.lastupdate,
t.opendate,
(select c.department_name from department c where c.department_id = t.categoryid) as category,
(select ts.status
from ticketstatus ts
where ts.ticketid = t.id
and ts.statusdate = (select max(ts2.statusdate)
from ticketstatus ts2
where ts.ticketid = ts2.ticketid)) as status
from tickets t
where t.user = '". $_SESSION['name'] ."'
and (select ts.status
from ticketstatus ts
where ts.ticketid = t.id
and ts.statusdate = (select max(ts2.statusdate)
from ticketstatus ts2
where ts.ticketid = ts2.ticketid)) <> 'Closed'
order by t.opendate desc";
$result = $mysqli->query($sql);
echo '<table class="table table-hover table-striped table-responsive"><th>User</th><th>Subject</th><th>Group</th><th>Created</th><th>Last update</th><th>Status</th>';
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
if ($row['status'] == 'Closed') { $class = "btn btn-danger";}
elseif ($row['status'] == 'Open') { $class = "btn btn-success";}
elseif ($row['status'] == 'Waiting on Client') {$class = "btn btn-info";}
else { $class = "btn btn-warning"; }
echo '<tr class="table_row">
<td>' .$row['user'] .'</td>
<td>' .$row['subject'] .'</td>
<td>' . $row['category'] . '</td>
<td>' . date("F j, Y, g:i a", strtotime($row['opendate'])) . '</td>
<td>' . time_ago($row['lastupdate']) . '</td>
<td><form method="POST">
<input name="ticketId" value="' . $row['ticketid'] . '" type="text" hidden />
<button type="submit" class="'.$class .'">'.$row['status'] .'</button></form>
</td>
</tr>';
}
} else {
echo '</table>';
$conn->close();
}
This is my function for time_ago get this error Call to undefined function time_ago() what did i do wrong?
function time_ago($ts)
{
$mins = floor((gmtime() - $ts)/60);
$hours = floor($mins/60);
$mins -= $hours*60;
$days = floor($hours/24);
$hours -= $days*24;
$weeks = floor($days/7);
$days -= $weeks*7;
if ($weeks == 1)
$w = "week";
elseif ($weeks > 1 && $weeks < 5)
$w = "weeks";
elseif ($weeks > 4)
$w = "weeks";
if ($weeks >= 1){
$we = "".$weeks." ".$w."";
}
if ($days == 1)
$d = "day";
elseif ($days > 1 && $days < 5)
$d = "day";
elseif ($days > 4)
$d = "days";
if ($days >= 1 ){
$da = "".$days." ".$d."";
}
if ($hours == 1)
$h = "hour";
elseif ($hours > 1 && $hours < 5)
$h = "hour";
elseif ($hours > 4)
$h = "hours";
if ($hours >= 1){
$ho = "".$hours." ".$h."";
}
if ($mins > 10 && $mins < 15)
$m = "minute";
elseif (($mins%10) == 1)
$m = "minute";
elseif (($mins%10) > 1 && ($mins%10) < 5)
$m = "minutes";
elseif (($mins%10) > 4)
$m = "minute";
if ($mins >= 1){
$mi = "".$mins." ".$m."";
}else{
$mi = "less than a minute";
}
if ($weeks || $days || $hours || $mins)
return $we." ".$da." ".$ho." ".$mi;
}

Compare 2 timestamps and set a statement based on the result

I have the following table:
-------------------------------------------
|id|list_id|start_date|end_date|min_nights|
-------------------------------------------
|17| 55 |1437487200|1437735600|3|
|18| 55 |1438005600|1438167600|2|
I want to display the minimum nights for each range.
PHP:
//Seasonal Price
//1. Store all the dates between checkin and checkout in an array
$checkin_time = get_gmt_time(strtotime($checkin));
$checkout_time = get_gmt_time(strtotime($checkout));
$travel_dates = array();
$seasonal_prices = array();
$total_nights = 1;
$total_price = 0;
$is_seasonal = 0;
$i = $checkin_time;
while ($i < $checkout_time) {
$i = get_gmt_time(strtotime('+1 day', $i));
$checkin_date = date('m/d/Y', $i);
$checkin_date = explode('/', $checkin_date);
$travel_dates[$total_nights] = $checkin_date[1] . $checkin_date[0] . $checkin_date[2];
$total_nights++;
}
for ($i = 1; $i < $total_nights; $i++) {
$seasonal_prices[$travel_dates[$i]] = "";
}
//Store seasonal price of a list in an array
$seasonal_query = $this->Common_model->getTableData('seasonalprice', array('list_id' => $id));
//vaild array checked ilan
$seasonal_result = $seasonal_query->result_array();
if ($seasonal_query->num_rows() > 0) {
foreach ($seasonal_result as $time) {
//Get Seasonal price
$seasonalprice_query = $this->Common_model->getTableData(
'seasonalprice', array(
'list_id' => $id,
'start_date' => $time['start_date'],
'end_date' => $time['end_date']
));
$seasonalprice = $seasonalprice_query->row()->price;
$seasonal_min_nights = $seasonalprice_query->row()->min_nights;
//Days between start date and end date -> seasonal price
$start_time = $time['start_date'];
$end_time = $time['end_date'];
$i = $start_time;
while ($i <= $end_time) {
$start_date = date('m/d/Y', $i);
$s_date = explode('/', $start_date);
$s_date = $s_date[1] . $s_date[0] . $s_date[2];
$seasonal_prices[$s_date] = $seasonalprice;
$i = get_gmt_time(strtotime('+1 day', $i));
}
}
Then there are the following conditions:
//half-day booking functionality
//add time up to 2PM
$checkin_timestamp = get_gmt_time(strtotime($checkin)) + (14 * 60 * 60);
//add time up to 11AM
$checkout_timestamp = get_gmt_time(strtotime($checkout)) + (11 * 60 * 60);
$query = $this->db->query('SELECT id, list_id FROM `calendar` WHERE `list_id` = "' . $id . '" AND (`booked_days` BETWEEN ' . $checkin_timestamp . ' AND ' . $checkout_timestamp . ') GROUP BY `list_id`');
$rows = $query->num_rows();
$daysexist = $this->db->query("SELECT id,list_id,booked_days FROM `calendar` WHERE `list_id` = '" . $id . "' AND (`booked_days` BETWEEN " . $checkin_timestamp . " AND " . $checkout_timestamp . ") GROUP BY `list_id`");
$rowsexist = $daysexist->num_rows();
if ($rowsexist > 0) {
echo '{"available":false,"total_price":' . $data['price'] . ',"reason_message":"Those dates are not available"}';
}
if ($data['guests'] > $capacity) {
echo '{"available":false,"total_price":' . $data['price'] . ',"reason_message":"' . $capacity . ' guest(s) only allowed"}';
}
elseif ($is_seasonal == 1 && $total_nights -1 < $seasonal_min_nights) {
echo '{"available":false,"total_price":"0","reason_message":"Minimum stay is ' . $data['min_nights_seasonal'] . ' nights for the following period: '.date('m/d/Y', $start_time).' - '.date('m/d/Y', $end_time).'"}';
exit;
}
$this->session->set_userdata("total_price_'" . $id . "'_'" . $this->dx_auth->get_user_id() . "'", $data['price']);
$staggered_price = "";
if ($days >= 30) {
$staggered_price = ',"staggered_price":"' . get_currency_symbol($id) . get_currency_value1($id, $data['price']) . '","staggered":false';
}
elseif (isset($extra_guest)) {
if ($extra_guest == 1) {
echo '{"service_fee":"' . get_currency_symbol($id) . get_currency_value_lys($row->currency, get_currency_code(), $data['commission']) . '","extra_guest_price":"' . get_currency_symbol($id) . get_currency_value1($id, $extra_guest_price) . '","extra_guest":1,"reason_message":"","price_per_night":"' . get_currency_symbol($id) . get_currency_value1($id, $per_night) . '","nights":' . $days . ',"available":true,"can_instant_book":false,"total_price":"' . get_currency_symbol($id) . get_currency_value1($id, $data['price']) . '"' . $staggered_price . '}';
}
}
else {
echo '{"service_fee":"' . get_currency_symbol($id) . get_currency_value_lys($row->currency, get_currency_code(), $data['commission']) . '","reason_message":"","price_per_night":"' . get_currency_symbol($id) . get_currency_value1($id, $per_night) . '","nights":' . $days . ',"available":true,"can_instant_book":false,"total_price":"' . get_currency_symbol($id) . get_currency_value1($id, $data['price']).'"}';
}
And the condition i setted up:
elseif ($is_seasonal == 1 && $total_nights -1 < $seasonal_min_nights) {
echo '{"available":false,"total_price":"0","reason_message":"Minimum stay is ' . $data['min_nights_seasonal'] . ' nights for the following period: '.date('m/d/Y', $start_time).' - '.date('m/d/Y', $end_time).'"}';
exit;
}
And it works fine as long as i have only 1 custom range, in the database there are 2 entries. but the condition i have made always show me the last information that was added to the database instead of the information that applies for the specific timestamp range.
How would i modify the condition to meet with the timestamps and retrive the min_nights foreach timestamps range? Thanks!
I have solved it myself, looped the timestamps and in the loop i setted up the variables i need to work with, then the condition applied to each range of the timestamp individually.. If anybody have similar situation:
Getting the min_nights
$seasonal_min_nights = $seasonalprice_query->row()->min_nights;
Looping in a while:
$start_time = $time['start_date'];
$end_time = $time['end_date'];
$i = $start_time;
while ($i <= $end_time) {
$start_date = date('m/d/Y', $i);
$s_date = explode('/', $start_date);
$s_date = $s_date[1] . $s_date[0] . $s_date[2];
$seasonal_nights[$s_date] = $seasonal_min_nights;
$i = get_gmt_time(strtotime('+1 day', $i));
}
And for loop:
//Total Price
for ($i = 1; $i < $total_nights; $i++) {
$min_seasonal_nights = $seasonal_nights[$travel_dates[$i]];
$is_seasonal = 1;
}
Finally the statment:
if ($is_seasonal == 1 && $total_nights - 1 < $min_seasonal_nights) {
echo '{"available":false,"total_price":"0","reason_message":"Minimum stay is ' . $data['min_seasonal_nights'] . ' nights for the following period: ' . date('m/d/Y', $data['s_start_date']) . ' - ' . date('m/d/Y', $data['s_end_date']) . '"}';
exit;
}
I would do other stype of coding for this but had to work with the existing.. so hope it helps anybody :)

PHP Calendar Issues

I have the following code that I created to generate a Calendar, but it has some issues:
//Labels
$dayLabels = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$monthLables = array("January","February","March","April","May","June","July","August","September","October","November","December");
//max values
$maxDays = 7;
$maxMonths = 12;
//stats
$forceMonth = $_GET['m'];
$forceYear = $_GET['y'];
$todayDate = date("d-m-Y");
$todayDate = date("d-m-Y", strtotime($todayDate));
$explodeToday = explode("-", $todayDate);
$currentDay = $explodeToday[0];
if(isset($forceMonth)) {
$currentMonth = $forceMonth;
} else {
$currentMonth = $explodeToday[1];
};
if(isset($forceYear)) {
$currentYear = $forceYear;
} else {
$currentYear = $explodeToday[2];
};
$daysInMonth = cal_days_in_month(CAL_GREGORIAN, $currentMonth, $currentYear);
//database values
$startDate = array("01-06-2015","25-06-2015");
$endDate = array("05-06-2015","05-07-2015");
$bookedUser = array("Dexter","James");
//counters
$daysIntoMonth = 0;
$dayCounter = 0;
//debug
echo '<p>Current Month: ' .$monthLables[$currentMonth-1]. ' / ' .$currentMonth. '</p>';
echo '<p>Current Year: ' .$currentYear. '</p>';
//start of Calendar
echo '<table>';
//print days of week
echo '<tr>';
foreach($dayLabels as $day) {
echo '<td style="border-bottom:dashed 1px #DDD;">' .$day. '</td>';
};
echo '</tr>';
while($daysIntoMonth < $daysInMonth) {
//days into month
$daysIntoMonth++;
$temp_inMonth = sprintf("%02d", $daysIntoMonth);
$daysIntoMonth = $temp_inMonth;
//days into week
$dayCounter++;
$temp_dayCounter = sprintf("%02d", $dayCounter);
$dayCounter = $temp_dayCounter;
//current calendar date
$calDate = date('d-m-Y', strtotime($daysIntoMonth. '-' .$currentMonth. '-' .$currentYear));
$timeCal = strtotime($calDate);
if($dayCounter == 1) {
echo '<tr>';
};
if($startKey = array_search($calDate, $startDate) !== FALSE) {
$booked = true;
};
if($endKey = array_search($calDate, $endDate) !== FALSE) {
$booked = false;
};
if($booked == true) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else if($booked == true && array_search($calDate, $startDate) !== FALSE) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else if($booked == false && array_search($calDate, $endDate) !== FALSE) {
echo '<td style="background-color:red;">' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
} else {
echo '<td>' .$calDate. ' / ' .$daysIntoMonth. ' ' .$dayCounter. '</td>';
}
if($dayCounter == $maxDays) {
echo '</tr>';
$dayCounter = 0;
};
};
//table is kill
echo '</table>';
The issues I have noticed:
Unable to put a $bookedUser for respective $startDate,$endDate.
When a booking laps over to another month, it skips all the dates until the $endDate.
All Months start on Monday, how would I go about making them start of correct days of the week.
Possible code examples to help me solve my issues would be great, thanks in advance.
Edit:
I have solved problem 3 by using the following code:
$firstDayofMonth = strtotime("01-$currentMonth-$currentYear");
$firstDayofMonth = date("D", $firstDayofMonth);
$firstDayofMonth = array_search($firstDayofMonth, $dayMiniLabels);
$firstDayofMonth = $firstDayofMonth + 1;
$startMonth = 0;
if($firstDayofMonth != 7) {
while($startMonth < $firstDayofMonth) {
echo '<td></td>';
$startMonth++;
$dayCounter++;
$temp_dayCounter = sprintf("%02d", $dayCounter);
$dayCounter = $temp_dayCounter;
};
};
For the days and months (problem 3), I would do this:
$todaysNumber = date('w');
$currentDayInText = $dayLabels[$todaysNumber];
And the same for the monhts.
Mostly, in my MySQL-tables, dates are placed like 2015-06-05 and not in European time notation. Maybe that could solve problem 1?

How to add row values together in a html table (calendar)?

I have a calendar I made with php using an html table.
Each date has an integer value inserted from the database below the day number.
What I want to do is get the sum of each row (week) of the table and put it in the 8th column. How can I do this?
Any help?
<?php
$conn = mysqli_connect('localhost','username','password','database_name');
?>
<html>
<head>
<title>
Calendar
</title>
<link rel="stylesheet" href="/test/style.css">
<script src="/test/script.js"></script>
</head>
<body>
<?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) ;
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
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td>
<td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>Total:</td></tr>";
$day_count = 1;
$row_number = 1;
echo "<tr id='row" . $row_number . "'>";
$row_number++;
//first we take care of those blank days
/////////get beginning of month
if($month-1 != 0) {
$last_month = $month-1;
}
else {
$last_month = 12;
}
if($last_month == '12') {
$year = $year-1;
}
$last_month_first_day = mktime(0,0,0,$last_month, 1, $year);
$last_month_days_in_month = cal_days_in_month(0, $last_month, $year);
$last_month_day_of_week = date('D', $last_mont_days_in_month);
$last_month_days_to_add_to_last_month_end = $blank;
$last_month_end = $last_month_days_in_month-$last_month_days_to_add_to_last_month_end;
//end ^^
while ( $blank > 0) {
echo "<td><span style='color:grey'>" . $last_month_end . "</span></td>";
$last_month_end++;
$blank = $blank-1;
$day_count++;
}
//sets the first day of the month to 1
$day_num = 1;
//count up the days, untill we've done all of them in the month
$week_total_mileage = array();
$x = 0;
while ( $day_num <= $days_in_month ) {
//get total miles from database
$getDay = $year . "-" . $month . "-" . $day_num;
$query = "SELECT * FROM table WHERE date='" . $getDay . "'";
$doQuery = mysqli_query($conn,$query);
while($rows = mysqli_fetch_assoc($doQuery)) {
$total_miles = $rows['total_miles'];
}
$num_rows = mysqli_num_rows($doQuery);
echo "<td id='" . $day_count . $row_number . "' value='" . $total_miles . "'
>
<form method='post' action='/test/day.php'>
<input type='hidden' value='" . $day_num . "' name='day'>
<input type='hidden' value='" . $title . "' name='month'>
<input type='hidden' value='" . $year . "' name='year'>
<input type='button' id='dayNum' value='" . $day_num . "'>
</form>
<span id='totalMiless'>Total miles: ";
if($num_rows == 1) {
echo $total_miles;
}
else {
echo '-';
}
echo "</span>
</td>
<div class='hiddenDay' id='" . $day_num . $title . $year . "' style='display:none'>
<span id='totalMiles'>Total miles: ";
if($num_rows == 1) {
echo $total_miles;
}
else {
echo '0';
}
echo "</span></div>";
$week_total_mileage[$day_num] = $total_miles;
$day_num++;
$day_count++;
if ($day_count > 7) {
$total_total = 0;
while($x < 8) {
$total_total = $total_total + $week_total_mileage[$x];
$x++;
}
while($x < 14 && $x > 8) {
$total_total = $total_total + $week_total_mileage[$x];
}
echo "<td>" . $total_total .
"</td></tr><tr id='row" . $row_number . "'>";
empty($week_total_mileage);
$day_count = 1;
$row_number++;
}
}
//Finaly we finish out the table with some blank details if needed
$end_days = 1;
while ( $day_count >1 && $day_count <=7 ) {
echo "<td><span id='endDays'>" . $end_days . "</span></td>";
$day_count++;
$end_days++;
}
echo "</tr></table>";
?>
</body>
</html>
The above code outputs this:
I just went through your script, put all total miles into an array
and calculate sum with
array_sum
Try this code:
<?php
$conn = mysqli_connect('localhost','username','password','database_name');
?>
<html>
<head>
<title>
Calendar
</title>
<link rel="stylesheet" href="/test/style.css">
<script src="/test/script.js"></script>
</head>
<body>
<?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) ;
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
echo "<table border=1 width=294>";
echo "<tr><th colspan=7> $title $year </th></tr>";
echo "<tr><td width=42>S</td><td width=42>M</td><td width=42>T</td>
<td width=42>W</td><td width=42>T</td><td width=42>F</td><td width=42>S</td><td width=42>Total:</td></tr>";
$day_count = 1;
$row_number = 1;
echo "<tr id='row" . $row_number . "'>";
$row_number++;
//first we take care of those blank days
/////////get beginning of month
if($month-1 != 0) {
$last_month = $month-1;
}
else {
$last_month = 12;
}
if($last_month == '12') {
$year = $year-1;
}
$last_month_first_day = mktime(0,0,0,$last_month, 1, $year);
$last_month_days_in_month = cal_days_in_month(0, $last_month, $year);
$last_month_day_of_week = date('D', $last_mont_days_in_month);
$last_month_days_to_add_to_last_month_end = $blank;
$last_month_end = $last_month_days_in_month-$last_month_days_to_add_to_last_month_end;
//end ^^
while ( $blank > 0) {
echo "<td><span style='color:grey'>" . $last_month_end . "</span></td>";
$last_month_end++;
$blank = $blank-1;
$day_count++;
}
//sets the first day of the month to 1
$day_num = 1;
//count up the days, untill we've done all of them in the month
$week_total_mileage = array();
$weekly_total = array();
$x = 0;
while ( $day_num <= $days_in_month ) {
//get total miles from database
$getDay = $year . "-" . $month . "-" . $day_num;
$query = "SELECT * FROM table WHERE date='" . $getDay . "'";
$doQuery = mysqli_query($conn,$query);
while($rows = mysqli_fetch_assoc($doQuery)) {
$total_miles = $rows['total_miles'];
}
$num_rows = mysqli_num_rows($doQuery);
echo "<td id='" . $day_count . $row_number . "' value='" . $total_miles . "'
>
<form method='post' action='/test/day.php'>
<input type='hidden' value='" . $day_num . "' name='day'>
<input type='hidden' value='" . $title . "' name='month'>
<input type='hidden' value='" . $year . "' name='year'>
<input type='button' id='dayNum' value='" . $day_num . "'>
</form>
<span id='totalMiless'>Total miles: ";
if($num_rows == 1) {
echo $total_miles;
}
else {
echo '-';
}
echo "</span>
</td>
<div class='hiddenDay' id='" . $day_num . $title . $year . "' style='display:none'>
<span id='totalMiles'>Total miles: ";
if($num_rows == 1) {
echo $total_miles;
}
else {
echo '0';
}
echo "</span></div>";
if(is_numeric($total_miles)) {
$weekly_total[] = $total_miles;
}
$week_total_mileage[$day_num] = $total_miles;
$day_num++;
$day_count++;
if ($day_count > 7) {
$total_total = 0;
while($x < 8) {
$total_total = $total_total + $week_total_mileage[$x];
$x++;
}
while($x < 14 && $x > 8) {
$total_total = $total_total + $week_total_mileage[$x];
}
echo "<td>" . array_sum($weekly_total) .
"</td></tr><tr id='row" . $row_number . "'>";
empty($week_total_mileage);
$weekly_total = array();
$day_count = 1;
$row_number++;
}
}
//Finaly we finish out the table with some blank details if needed
$end_days = 1;
while ( $day_count >1 && $day_count <=7 ) {
echo "<td><span id='endDays'>" . $end_days . "</span></td>";
$day_count++;
$end_days++;
}
echo "</tr></table>";
?>
</body>
</html>

PHP calendar with left write navigation

I'm trying to create a calendar with only PHP.
Here is what I have done:
<?pop
$date = time():
$day = date("d", $date);
$month = date("m", $date);
$year = date("y", $date);
$firstDay = mktime(0, 0, 0, $month, 1, $year);
$title = date("F", $firstDay);
$dayOfWeek = date("D", $firstDay);
switch($dayOfWeek) {
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;
}
$daysInMonth = cal_days_in_month(0, $month, $year);
echo "<table>";
echo "<tr><th>" . $title . " " . $year "</th></tr>";
echo "<tr><td>Sun</td><td>Mon</td><td>Tue</td><td>Wed</td><td>Thu</td><td>Fri</td><td>Sat</td></tr>";
$dayCount = 1;
while ($blank > 0) {
echo "<td></td>";
$blank--;
$dayCount++;
}
$dayNum = 1;
while ($dayNum <= $daysInMonth) {
echo "<td>" . $dayNum . "</td>";
$dayNum++;
$dayCount++;
if ($dayCount > 7) {
echo "</tr><tr>";
$dayCount = 1;
}
}
while ($dayCount > 1 and $dayCount <= 7) {
echo "<td></td>";
$dayCount++;
}
echo "</tr></table>";
?>
The logic seems to be fine (at leaset to me for now). However, when I tried this on my server, I got this error message:
"; echo "" . $title . " " . $year ""; echo "SunMonTueWedThuFriSat"; $dayCount = 1; while ($blank > 0) { echo ""; $blank--; $dayCount==; } $dayNum = 1; while ($dayNum <= $daysInMonth) { echo "" . $dayNum . ""; $dayNum++; $dayCount++; if ($dayCount > 7) { echo ""; $dayCount = 1; } } while ($dayCount > 1 and $dayCount <= 7) { echo ""; $dayCount++; } echo ""; ?>
I have absolutely no idea why this happened. I would like to ask for you help on this.
Also, I wonder if I can add two arrow so the user can click on them in order to go to the next or previous months using only PHP.
Thanks
Just Check what you've written in the PHP opening tag at the begining of the file... It has to be
<?php not <?pop
Do Refer this PHP Basic Syntax which explains the basic PHP syntax

Categories