PHP table based on month and date - php

i want make a table than can be input with existing data, and also the table already exist even before the data inputted,table created based on how many days in this month(i.e: right now is october so it have 31 tables since october have 31 days).
The question are i want to put date number in column date and days in column days, basically date =1 so days of week=saturday, date =2 so days of week=sunday(based on current calendar) and so on
i not quite sure about the logic, cause every time i tried, it only fill the first div or its fill the whole divcheck out my code below
CODE
<table class="table table-bordered">
<thead>
<tr>
<th rowspan="2">Action</th>
<th rowspan="2">Date</th>
<th rowspan="2">Day of Week</th>
<th rowspan="2">Location</th>
<th rowspan="2">Brief Description of Activity </th>
<th colspan="6"><center>Project Code & Hour Worked</center> </th>
</tr>
<tr>
<th>A</th>
<th>Hours Worked</th>
<th>B</th>
<th>Hours Worked</th>
<th>C</th>
<th>Hours Worked</th>
</tr>
</thead>
<tbody>
<?php
$start = new DateTime('first day of this month');
$end = new DateTime('first day of this month + 1 month');
$period = new DatePeriod($start, new DateInterval('P1D'), $end);
for($i= 1; $i < date('t') + 1; $i++){
for($j =1;$j<=1;$j++){
echo "<td><a href='#modal-dialog' class='btn btn-xs btn-success' data-toggle='modal'><span class='fa fa-pencil'></span>i=".$i."</td>".PHP_EOL;
}
for($j =2;$j<=2;$j++){
foreach($period as $day){
echo "<td>".$day->format('M-d')."</td>".PHP_EOL;
}
for($j =1;$j<11;$j++){
echo "<td></td>".PHP_EOL;;
}
echo "</tr>";
}
?>
</tbody>

You are doing it wrong. You loop through the days and build the columns. Your loop through the days should build the rows.
<tbody>
<?php
$day = date('Y-m-d',strtotime('first day of this month'));
for($i= 1; $i < date('t') + 1; $i++){
?>
<tr>
<td><a href='#modal-dialog' class='btn btn-xs btn-success' data-toggle='modal'><span class='fa fa-pencil'></span>i=<?php echo $i; ?></td>
<td><?php echo date('M-d',strtotime($day)); ?></td>
<td><?php echo date('l',strtotime($day)); ?></td>
<?php
for($j =1;$j<=8;$j++){
echo "<td></td>".PHP_EOL;
}
?>
</tr>
<?php
$day = date('Y-m-d', strtotime('+1 day', strtotime($date)));
} ?>
</tbody>

Related

How do I create a menu table

I am very new to mysql and php. I'm making a restaurant menu. the problem is that on days the dishes are not in a row, they slip. what am I doing wrong?
I want my table to be like this table:
My code:
<table>
<tr>
<th><?php $d1=strtotime("monday this week");
echo date("Y-m-d", $d1)?></th>
<th><?php $d2=strtotime("tuesday this week");
echo date("Y-m-d", $d2)?></th>
<th><?php $d3=strtotime("wednesday this week");
echo date("Y-m-d", $d3)?></th>
<th><?php $d4=strtotime("thursday this week");
echo date("Y-m-d", $d4)?></th>
<th><?php $d4=strtotime("friday this week");
echo date("Y-m-d", $d4)?></th>
</tr>
<?php
while($rows=$result->fetch_assoc())
{
?>
<tr>
<td class="px-2 bg-success"><?php if($rows['datum'] == date("Y-m-d", $d1)){
echo $rows['nev'];}?></td>
<td class="px-2 bg-success"><?php if($rows['datum'] == date("Y-m-d", $d2)){
echo $rows['nev'];}?></td>
<tr>
<?php
}
?>
</table>
Thanks very much!
This is the current table:

Difference between two dates in mysql & PHP

I want to know the difference between two dates(joinon in table's field) of different id's in same table in PHP. Attachement of my table structure as attached below.
Displayed dates are shown at the mentioned fields shown at the below screen shots.
[![enter image description here][2]][2]
my php code are mentioned below
<table id="dataTableExample1" class="table table-bordered table-striped table-hover">
<thead>
<tr class="info">
<th class="text-center">Days</th>
<th>Date</th>
<th>Title</th>
<th>Time Interval</th>
<th>Attachment</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$proj_id = $_GET['proj_id'];
$proj = mysql_query("select * from project_historytl where proj_id='$proj_id' order by proj_histl_id desc") or die(mysql_error());
///echo "select * from project_historytl where proj_histl_id='$proj_id' order by proj_histl_id desc";exit();
$s = 0;
while ($getannexure = mysql_fetch_array($proj))
{
$s++;
?>
<tr>
<td class="text-center">Day <?php echo $s;?></td>
<td><?php echo $getannexure['joinon']; ?></td>
<td><?php echo $getannexure['proj_history_title']; ?></td>
<td> </td>
<td>View </td>
<td>
<span class="elusive icon-pencil" title="edit"></span><i class="fa fa-pencil"></i>
<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#customer2"><i class="fa fa-trash-o"></i> </button>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
I am getting the result like this format enter image description here
The logic of comparing two dates is as follows:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
In your case, it's probably easier if you change the structure a little so that all the array rows are stored in a single array before you start comparing dates.
Replace your while() loop with the following:
while ($getannexure[] = mysql_fetch_array($proj, MYSQL_ASSOC)){}
The run the following foreach():
foreach($getannexure as $id => $row) {
if (!empty($getannexure[$id + 1])) {
$datetime1 = new DateTime($row[$id + 1]['joinon']);
$datetime2 = new DateTime($row[$id]['joinon']);
$interval = $datetime1->diff($datetime2);
$getannexure[$id]['diff'] = $interval->format('%R%a days');
} else {
$getannexure[$id]['diff'] = NULL;
}
}
Now loop thru the $getannexure array like you have and you'll have an added diff value that you can use.

how to subtract 1 month from any date and fetch value of that month from database

$payment_date is date from which I want to subtract 1 month.
how could I do ?
<table class="table table-bordered table-hover" id="dataTables-example">
<thead>
<tr>
<th>Payment Month</th>
<th>Payment Date</th>
<th>Gross Salary </th>
<th>Total Deduction</th>
<th>Net Salary</th>
<th>Advance Salary</th>
<th>Incentive</th>
<th>Fine Deduction</th>
<th>Payment Amount</th>
<th class="hidden-print">Details</th>
</tr>
</thead>
<tbody>
<?php
if (!empty($payment_history)): foreach ($payment_history as $v_payment_history) :
?>
<tr>
<td><?php echo date('F-Y', strtotime($v_payment_history->payment_for_month)); ?></td>
<td><?php echo date('d-M-y', strtotime($v_payment_history->payment_date)); ?></td>
<td><?php echo $gross = $v_payment_history->basic_salary + $v_payment_history->house_rent_allowance + $v_payment_history->medical_allowance + $v_payment_history->special_allowance + $v_payment_history->fuel_allowance + $v_payment_history->phone_bill_allowance + $v_payment_history->other_allowance; ?></td>
<td><?php echo $deduction = $v_payment_history->tax_deduction + $v_payment_history->provident_fund + $v_payment_history->other_deduction; ?></td>
<td><?php echo $net_salary = $gross - $deduction; ?></td>
<td><?php echo $v_payment_history->award_amount; ?></td>
<td><?php echo $v_payment_history->incentive; ?></td>
<td><?php echo $v_payment_history->fine_deduction; ?></td>
<td><?php echo $v_payment_history->payment_amount; ?></td>
<td class="hidden-print"><?php echo btn_view('admin/payroll/salary_payment_details/' . $v_payment_history->salary_payment_id) ?></td>
</tr>
<?php
endforeach;
?>
<?php else : ?>
<tr>
<td colspan="9">
<strong>There is no data for display</strong>
</td>
</tr>
<?php endif; ?>
</tbody>
</table>
$payment_date is date from which I want to subtract 1 month.
how could I do ? I want payment only of last month from the month I'm giving payment
Easy!
<?php
$date = new DateTime();
$date->modify('-1 month');
echo $date->format('Y-m-d H:i:s');
See it here https://3v4l.org/UWaGY
You can also feed in a date string as a constructor. new DateTime('2014-09-18');
#LEARNER try it like this, check the output and code online here https://eval.in/806939:
<?php
//suppose that date is 2017/05/29
$yourDate = strtotime('2017/05/29 -1 month'); // substract 1 month from that date and converting it into timestamp
$desiredDate = date("Y-m-d", $yourDate);
echo $desiredDate;
?>

Displaying date from monday to sunday

I want to make a table weekly schedule , and I need to attach the dates in html markup. So far I got this code :
<table class="schedule-table">
<tr class="days-tr">
<td>
<span><?php echo $date = date("dS",strtotime('monday this week')).' - '.date("dS",strtotime("sunday this week")); ?> </span>
<span> <?php echo $date = date("M-Y",strtotime('monday this week')); ?></span>
</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('monday this week')); ?>">Monday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('tuesday this week')); ?>">Tuesday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('wednesday this week')); ?>">Wednesday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('thursday this week')); ?>">Thursday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('friday this week')); ?>">Friday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('saturday this week')); ?>">Saturday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('sunday this week')); ?>">Sunday</td>
</tr>
</table>
And here is the output :
<table class="schedule-table">
<tbody>
<tr class="days-tr">
<td>
<span>16th - 15th </span>
<span> Mar-2015</span>
</td>
<td data-workout-day="16-Mar-2015">Monday</td>
<td data-workout-day="10-Mar-2015">Tuesday</td>
<td data-workout-day="11-Mar-2015">Wednesday</td>
<td data-workout-day="12-Mar-2015">Thursday</td>
<td data-workout-day="13-Mar-2015">Friday</td>
<td data-workout-day="14-Mar-2015">Saturday</td>
<td data-workout-day="15-Mar-2015">Sunday</td>
</tr>
</tbody>
</table>
As you can see it takes the Monday date from following week instead of 9-Mar-2015 ? How to fix that to display it properly? (for example if we have Friday 13-Mar i want the mon, tues, thur to have dates from previous week not the following.
If you are assuming that you week is starting from monday
then first of all check
$day = date('l')
if($day != 'Monday'){
echo $date = date("d-M-Y",strtotime( "previous monday" ));
}
strtotime is hugely powerful and can accept a lot of strings to get the desired date. In this case, you want this week to get this weeks dates.
By changing 'next monday' etc to 'this week monday' you get the dates for Monday of this current week:
<table class="schedule-table">
<tr class="days-tr">
<td>
<span><?php echo $date = date("dS",strtotime('this week')).' - '.date("dS",strtotime("this week sunday")); ?> </span>
<span> <?php echo $date = date("M-Y",strtotime('monday this week')); ?></span>
</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week monday')); ?>">Monday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week tuesday')); ?>">Tuesday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week wednesday')); ?>">Wednesday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week thursday')); ?>">Thursday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week friday')); ?>">Friday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week saturday')); ?>">Saturday</td>
<td data-workout-day="<?php echo $date = date("d-M-Y",strtotime('this week sunday')); ?>">Sunday</td>
</tr>
</table>
Generates:
<table class="schedule-table">
<tbody>
<tr class="days-tr">
<td>
<span>09th - 15th </span>
<span> Mar-2015</span>
</td>
<td data-workout-day="09-Mar-2015">Monday</td>
<td data-workout-day="10-Mar-2015">Tuesday</td>
<td data-workout-day="11-Mar-2015">Wednesday</td>
<td data-workout-day="12-Mar-2015">Thursday</td>
<td data-workout-day="13-Mar-2015">Friday</td>
<td data-workout-day="14-Mar-2015">Saturday</td>
<td data-workout-day="15-Mar-2015">Sunday</td>
</tr>
</tbody>
</table>

Structuring MySQL query efficiently

I'm looking for some help in how best to structure a MySQL query and display the results. I am trying to create a table on a PHP page which has 7 dates across the top and room numbers down the side. In the table, if a person is in a room on the given date then their name appears. I have the following tables:
Table: rooms
Fields: id, room
Table: bookings
Fields: id, name, room, checkin, checkout
The code I currently have is:
<? $start = date('Y-m-d');
$end = date('Y-m-d', strtotime($start." + 6 day")); ?>
<table class="table1">
<thead>
<tr>
<th></th>
<th scope="col"><?= date('d-m', strtotime($start)); ?></th>
<th scope="col"><?= date('d-m', strtotime($start." + 1 day")); ?></th>
<th scope="col"><?= date('d-m', strtotime($start." + 2 day")); ?></th>
<th scope="col"><?= date('d-m', strtotime($start." + 3 day")); ?></th>
<th scope="col"><?= date('d-m', strtotime($start." + 4 day")); ?></th>
<th scope="col"><?= date('d-m', strtotime($start." + 5 day")); ?></th>
<th scope="col"><?= date('d-m', strtotime($start." + 6 day")); ?></th>
</tr>
</thead>
<tbody>
<? $q1 = mysqli_query($con,"SELECT * FROM rooms GROUP BY room");
while($row1 = mysqli_fetch_assoc($q1)){ ?>
<tr>
<th><?= $row1['room']; ?></th>
<? $i = 0;
while ($i <= 6) { ?>
<td>
<? $q2 = mysqli_query($con,"SELECT * FROM bookings WHERE ((checkin BETWEEN '$start' and '$end') or (checkout BETWEEN '$start' and '$end') or (checkin <= '$start' AND checkout >= '$end')) and room = '$row1[room]'");
while($row2 = mysqli_fetch_assoc($q2)){
if ($row2['checkout'] > date('Y-m-d', strtotime($start." + ".$i." day")) and $row2['checkin'] <= date('Y-m-d', strtotime($start." + ".$i." day"))) {
echo $row2['name'];
}
} ?>
</td>
<? ++$i;
} ?>
</tr>
<? } ?>
</tbody>
</table>
This code currently produces the result I need however, I'm worried that it's inefficient because of the amount of loops and queries it runs to get the data.
Can anyone suggest a better way of formatting the query/result?
I think the logic you want in the where clause is:
checkin <= '$end' AND checkout >= '$start' and room = '$row1[room]'
A room is occupied between the two dates when the checking is before the end date and the checkout is after the start date.
EDIT:
The clause you have is:
checkin <= '$start' AND checkout >= '$end'
That is different from my solution.

Categories