How to compare days with current days in php? - php

<table class="table row-border order-column">
<thead>
<tr>
<th></th>
<?php
$days = cal_days_in_month(CAL_GREGORIAN, $month, $year);
for($i=1;$i<=$days;$i++)
{
?>
<th><?php echo $i; ?></th>
<?php
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ($student as $key => $value)
{
?>
<tr>
<td class="cap"><?php echo $value->name; ?></td>
<?php
for($i=1;$i<=$days;$i++)
{
$result = $this->db->select('*')->from('attendance')->where('studentID',$value->studentID)->where('day(date)', date($i))->where('month(date)', date($month))->where('Year(date)', date($year))->get()->row();
if(empty($result))
{
$date = date('Y-m-d');
$current_date = date('d',strtotime($date));
if($i < $current_date)
{
?>
<td class="absent">A</td>
<?php
}
else
{
?>
<td></td>
<?php
}
}
else
{
$dateday = date('d',strtotime($result->date));
if($i == $dateday)
{
?>
<td class="present">P</td>
<?php
}
}
}
?>
</tr>
<?php
}
?>
</tbody>
In the above code I want to show absent while compare $current_date and $days but now what happen here only current month date show absent and previous month date show blank. Now, What am I going to do I don't want to show absent in future date. So, How can I do this? Please help me.
Thank You

Use Unix timestamp for your calculation and after that change Unix to gmt or utc time using ready functions to show.

Related

Getting HTML table to display in one row

Currently I have this,
This my code
<table class="table table-hover">
<thead>
<tr>
<th scope="col">Monday</th>
<th scope="col">Tuesday</th>
<th scope="col">Wednesday</th>
<th scope="col">Thursday</th>
<th scope="col">Friday</th>
<th scope="col">Saturday</th>
<th scope="col">Sunday</th>
</tr>
</thead>
<tbody>
<?php
if(isset($_GET['class_id'])) {
$class_id = $_GET['class_id'];
} else {
$class_id = '1'; //default class id => Yoga
}
$mQuery = "SELECT * FROM timetable where class_id = '".$class_id."'"; //code to select from database
$result = $pdo-> query($mQuery);
while($row = $result->fetch(PDO::FETCH_ASSOC)):
$timeslot = $row['timeslot'];
// pass delimiter and string to explode function
$timeslot_ar = explode(',', $timeslot);
?>
//code to display result in table
<tr class="table-active">
<?php if(date('l', strtotime($row['date'])) == 'Monday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Tuesday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Wednesday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Thursday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Friday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Saturday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
<?php if(date('l', strtotime($row['date'])) == 'Sunday') { ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php }else { ?>
<td></td>
<?php } ?>
</tr>
<?php endwhile; ?>
</tbody>
</table>
I've been trying to get my result to display in a single row for all the days and then make a new row when a day of the same name already exists. I've been trying to figure out the login behind this and after countless of stack overflow answers and youtube tutorials I got to this point. I still can't manage to get them in a single row, if anyone would be kind enough to explain the method behind this, I would really appreciate it.
Put the <tr></tr> outside of the while() loop:
<tr class="table-active">
<?php
// other code
while($row = $result->fetch(PDO::FETCH_ASSOC)):
//code for table cells
?>
</tr>
I'm going to assume the timetable is output in date order in the query. You should probably include an ORDER BY clause to make sure of that. I also assume that the results may cover more than one week. Lastly I'm going to assume that every single day is represented by a row in the results. If it's not the case, then please show a sample of results which demonstrates the reality (as you will need some extra logic to deal with missing days).
Using those assumptions, it looks like you only need to start a new row when the current day is Monday, and end it when it's Sunday. So...move the row creation inside the if for Mondays, and the ending inside the if for Sundays, e.g.:
<?php if(date('l', strtotime($row['date'])) == 'Monday') { ?>
<tr class="table-active">
...
<?php if(date('l', strtotime($row['date'])) == 'Sunday') { ?>
</tr>
...
Also, it looks like you actually output the exact same <td> for each day, and you don't want to output blank tds if it's not the mentioned day (because that will add extra cells you don't need in the row), so the whole loop could be simplified as follows:
while($row = $result->fetch(PDO::FETCH_ASSOC)):
$timeslot = $row['timeslot'];
// pass delimiter and string to explode function
$timeslot_ar = explode(',', $timeslot);
?>
//code to display result in table
<?php if(date('l', strtotime($row['date'])) == 'Monday') { ?>
<tr class="table-active">
<?php } ?>
<td><?= date('d M', strtotime($row['date'])); ?><br/><?= implode("<br/>", $timeslot_ar); ?></td>
<?php if(date('l', strtotime($row['date'])) == 'Sunday') { ?>
</tr>
<?php } ?>
<?php endwhile; ?>
You can also replace the implode/explode with a simple string replacement function...I'll leave that as an exercise for the reader.
Managed to fix my issue by scraping off the table and using custom div containers.
Code snippet:
<div class="card" style="width: 9rem;display:inline-block">
<ul class="list-group list-group-flush">
<center><li class="list-group-item"><b>Monday</b></li></center>
<?php
$sql = "SELECT * FROM timetable where class_id='$class_id' and day='Monday'";
$stm = $pdo->query($sql);
$slots = $stm->fetchAll();
foreach ($slots as $row) {
$timeslot = $row['timeslot'];
// pass delimiter and string to explode function
$timeslot_ar = explode(',', $timeslot);
echo '<center>'.date('d M', strtotime($row['date'])).'</center>'.'<li class="list-group-item">'.implode("<br/>", $timeslot_ar). '</li>';
}
echo "<br/>";
?>
</ul>
</div>
Final product

php foreach combine 2 array into 1 row (workdays & data)

I have a problem with my current "Attendance Project", so I have 2 arrays.
1st array is to show a "workdays"
the 1st array show only workdays in current month ex:April, so the result in my 1st array is (3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28)
2nd array is showing Employee Attendance in current month ex:April, so the result in my 2nd array is (17, 19)
here is my current code :
<table class="table table-striped table-bordered zero-configuration">
<thead>
<tr>
<th style="width: 200px">Siswa</th>
<!-- <?php for($i = 1; $i < 31; ++$i){?>
<th><?= $i ?></th>
<?php } ?> -->
<?php foreach($workdays as $w){ ?>
<th><?=$w;?></th>
<?php } ?>
</tr>
</thead>
<tbody>
<?php
// for($x = 1; $x < 27; ++$x){
foreach($records as $r){
?>
<tr>
<td style="width: 200px"><?=$r->StudentName;?></td>
<?php
?>
<?php
foreach($workdays as $w){
foreach($tanggale as $t){
if($w == $t){
?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } } } ?>
</tr>
<?php } ?>
</tbody>
</table>
It will produce :
I want value (17 and 19) will markup the data with yellow background, and the table is NOT out of range.
Any help will appreciate..
Your code seems messy and I'm not gonna try to fix it on what you have, but I'll suggest solution:
1st - run foreach ($workdays as $w) and make header
2nd - run foreach ($workdays as $w) and make table-body like:
foreach ($workdays as $w) {
if (in_array($w, $tanggale)) //if tanggle is the one with 17 and 19
{
//code
}
else
{
//code
}
}
Simply what u can do is combine the 2 arrays in to one and then iterate the combine array as per your requirment.
Check below code for combining the array
<?php
$working_days = array(3,4,5,6,7,10,11,12,13,14,17,18,19,20,21,24,25,26,27,28);
$present_days = array(17.19);
$combine_attendence_array = array();
foreach($working_days as $day) {
$combine_attendence_array[$day] = 'Absent';
if(in_array($day, $present_days)) {
$combine_attendence_array[$day] = 'Present';
}
}
?>
This code will create combine array with key as day and value is present or Absent.
Now you can iterate as per your requirement below is the iteration code.
foreach($combine_attendence_array as $day => $value){
if($value == 'Present'){ ?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } ?>
<?php } ?>
I hope this answers solves your question.
Do it in this way
<?php
foreach($tanggale as $t){
if(in_array($t,$workdays)){
?>
<td style="background: #FFF000">M</td>
<?php }else{ ?>
<td style="background: #48C9A9">O</td>
<?php } } ?>
</tr>
<?php } ?>
foreach($workdays as $w){
foreach($tanggale as $t){
if($w == $t){
$color = "#FFF000";
$text = "M";
} else {
$color = "#48C9A9";
$text = "O";
}
}
?>
<td style="background: <?php echo $color; ?>"><?php echo $text; ?></td>
<?php }?>

create dummy date array and show data from databese table by month and year php

I have problem when showing in HTML, my date array always duplicate when I insert a new record. I already searching for this problem but I still don't get any answer.
$list=array();
$month = #$_POST['month'];
$year = #$_POST['year'];
$show = #$_POST['show'];
for($d=1; $d<=31; $d++) {
$time=mktime(12, 0, 0, $month, $d, $year);
if (date('m', $time)==$month)
$list[]=date('d-M-Y, D', $time);
}
foreach ($list as $day) {
$query = mysql_query("SELECT * FROM tb_attendance
WHERE tb_attendance.employee_number = '$employee_number' GROUP BY badge_number, date_in")
or die (mysql_error());
while ($data = mysql_fetch_array($query)) { ?>
<tr>
<td> <?php echo $day; ?> </td>
<?php if (date('d/m/Y', strtotime($day)) == $data['date_in']) { ?>
<td> <?php echo $data['employee_number']; ?> </td>
<td> <?php echo $data['badge_number']; ?> </td>
<td> <?php echo $data['employee_name']; ?> </td>
<td> <?php echo $data['time_in']; ?> </td>
<td> <?php echo $data['time_out']; ?> </td>
<?php } elseif (date('d/m/Y', strtotime($day)) != $data['date_in']) { ?>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<?php }}} ?>
This below is the layout in HTML:
These are the tables (tb_logdata & tb_attendance)

PHP calendar with dates --> distinct weeks and merging rows

I am currently creating a calendar where you can see all users that are saved in the database for that day and the possibility to add an user per date.
I want to work with dates because I think it is easier to work with in the future.
So what I am doing: I save a date into the database combined to an userid.
While I am fetching the data from database into a table I am converting the dates to days and weeks and years. Just to see which day is at 2015/10/05.
So I get $day = Monday, $week = 41, $year = 2015
In the table I want to put the users to the matching day.
The calendar looks like this:
I am calling an if statement:
if ($day == 'Mon') {
// SELECT * FROM users WHERE datum=$date
// Fetch and echo the users name into that day
}
The database looks like this:
The weeks showed here in the database, maybe they can be used for a proper display? But I don't know I tried several things.
I am using this code for putting the database rows into a table:
$sql = "SELECT distinct(week),datum, van , tot FROM roosternew ORDER BY datum ASC";
$run_sql = mysqli_query($conn , $sql);
?>
<table class="table">
<thead>
<tr class="primary">
<th width="5%"><center>Week</center></th>
<th width="10%">Maandag</th>
<th width="10%">Dinsdag</th>
<th width="10%">Woensdag</th>
<th width="10%">Donderdag</th>
<th width="10%">Vrijdag</th>
<th width="10%">Zaterdag</th>
<th width="10%">Zondag</th>
</tr>
</thead>
<tbody>
<?php while ($row = mysqli_fetch_assoc($run_sql)) {
echo $row['datum'].'test';
$datum = $row['datum'];
$van = $row['van'];
$tot = $row['tot'];
$tijd = $van.' - '.$tot;
$date = new DateTime($datum);
$week = $date->format("W");
$dag = $date->format("D");
$jaar = $date->format("Y");
$newdate_ = new DateTime($datum);
$newdate = $newdate_->format('Y-m-d');
$week_start = new DateTime();
$week_start->setISODate($jaar,$week);
$thisdate_ = $week_start->format('d-m');
//Create plus button
$plus_button = ' <i class="fa fa-plus-square"></i><br/>';
//Plus button
$thisdate = '<div class="datetable"><div class="datetable">'.$thisdate_.'</div></div><br/>';
?>
<tr>
<td class="primary"><center><strong><?= $week; ?></strong><br/><?php echo $thisdate.'<h4><small>'.$jaar.'</small></h4>'; ?></center></td>
<td>
<?php
createPlusButton(0, $jaar,$week);
if ($dag == 'Mon') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(1, $jaar,$week);
if ($dag == 'Tue') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(2, $jaar,$week);
if ($dag == 'Wed') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(3, $jaar,$week);
if ($dag == 'Thu') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(4, $jaar,$week);
if ($dag == 'Fri') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(5, $jaar,$week);
if ($dag == 'Sat') {
getScheduleAdmin($newdate);
}
?>
</td>
<td>
<?php
createPlusButton(6, $jaar,$week);
if ($dag == 'Sun') {
getScheduleAdmin($newdate);
}
?>
</td>
</tr>
<? } ?>
</tbody>
</table>
The function getScheduleAdmin($newdate):
function getScheduleAdmin($newdate) {
require('connect.php');
$sql2 = "SELECT * FROM roosternew WHERE datum='$newdate'";
$run_sql2 = mysqli_query($conn , $sql2);
while ($row2 = mysqli_fetch_assoc($run_sql2)) {
$id_rooster_users = $row2['idusers'];
$sql= mysqli_query($conn,"SELECT * FROM users WHERE idusers='$id_rooster_users'");
$row = mysqli_fetch_array($sql);
$name = $row['name'];
$color = $row['color'];
$sql3 = "SELECT van,tot FROM roosternew WHERE idusers= '$id_rooster_users'";
$sql3_run = mysqli_query($conn,$sql3);
$sql3_array = mysqli_fetch_array($sql3_run);
$van_user = $sql3_array['van'];
$tot_user = $sql3_array['tot'];
$tijd_user = $van_user.' - '.$tot_user;
echo ''.$name.'<br/>'.'('.$tijd_user.')';
echo '<br/>';
}
}
The problem is described in the first image (the calendar), it will show me more than once the week and they won't merge.
I dont know how to merge the weeks so that it will be :
Something like that.
And not like this (situation right now):
if anyone knows a better way of doing this, please tell me.

Something about php calendar

I am a new learner of PHP, now I want to use it to do a calendar like the following. There are something that I don't know.
How to get the first day of the month is sunday or another?
How to output all the days of the month and emphasize today?
The code:
<table cellspacing="0" cellpadding="3" bordercolor="#000000" border="1" style="width: 105px; border-collapse: collapse;">
<tbody>
<tr>
<td colspan="7"><?php echo date('F')." ".date('Y');?></td>
</tr>
<tr>
<td>Su</td>
<td>M</td>
<td>Tu</td>
<td>W</td>
<td>Th</td>
<td>F</td>
<td>Sa</td>
</tr>
<?php
$numrows = ceil(date('t')/7);
for($k=1;$k<=$numrows;$k++){
?>
<tr><td></td></tr>
<?php }?>
</tbody>
</table>
I don't know how to output the following days.
First Day of Month
date('d', strtotime('2012-04-01'));
Replace year and month with actual year and month you want to check. Leave the 01 for the first day.
http://php.net/manual/en/function.date.php
Output Days of Month and Emphasize Today
You can use date() with no second parameter to get the current time formatted however you want. To output the days of the month, you'd need to know how you wanted them formatted, so that's up to you to figure out. But, to get the number of days in a given month, you can use:
date('t')
That will return a number between 28 and 31, depending on how long the month is.
You really should read this: http://www.php.net/manual/en/ref.datetime.php
You never need these things nowadays when http://jqueryui.com/demos/datepicker/ is available, but if you want here is the code I wrote about 10 years ago, lol :)
<?
function my_calendar($fill=array()) {
if (isset($_GET['y'])) $y=$_GET['y'];
if (isset($_GET['m'])) $m=$_GET['m'];
if (isset($_GET['date']) AND strstr($_GET['date'],"-")) list($y,$m)=explode("-",$_GET['date']);
if (!isset($y) OR $y < 1970 OR $y > 2037) $y=date("Y");
if (!isset($m) OR $m < 1 OR $m > 12) $m=date("m");
$month_stamp=mktime(0,0,0,$m,1,$y);
$month_name=date("M",$month_stamp);
$day_count=date("t",$month_stamp);
$weekday=date("w",$month_stamp);
if ($weekday==0) $weekday=7;
$start=-($weekday-2);
$last=($day_count+$weekday-1) % 7;
if ($last==0) $end=$day_count; else $end=$day_count+7-$last;
$today=date("Y-m-d");
$prev=date('?\m=m&\y=Y',mktime (0,0,0,$m-1,1,$y));
$next=date('?\m=m&\y=Y',mktime (0,0,0,$m+1,1,$y));
$i=0;
?>
<table border=1 cellspacing=0 cellpadding=2>
<tr>
<td colspan=7>
<table width="100%" border=0 cellspacing=0 cellpadding=0>
<tr>
<td align="left"><<<</td>
<td align="center"><? echo $month_name," ",$y ?></td>
<td align="right">>>></td>
</tr>
</table>
</td>
</tr>
<tr><td>Mon</td><td>Tue</td><td>Wed</td><td>Th</td><td>Fri</td><td>Sat</td><td>Sun</td><tr>
<?
for($d=$start;$d<=$end;$d++) {
if (!($i++ % 7)) echo " <tr>\n";
echo ' <td align="center">';
if ($d < 1 OR $d > $day_count) {
echo "&nbsp";
} else {
$now="$y-$m-".sprintf("%02d",$d);
if (is_array($fill) AND in_array($now,$fill)) {
echo '<b>'.$d.'</b>';
} else {
echo $d;
}
}
echo "</td>\n";
if (!($i % 7)) echo " </tr>\n";
}
?>
</table>
<? } ?>
Use example
<?
if (isset($_GET['date'])) echo "Date picked: ".$_GET['date'];
my_calendar(array(date("Y-m-d")));
?>
it displays Monday first though. You need to remove some lines to get it to Sunday

Categories