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.
Related
I am trying to compute the differences between array values in PHP in a while loop.
Initially I was using a foreach loop using (next) and (prev) but it didn't seem to work very well.
What I am trying now actually works but the problem is the value that is calculated should actually appear on the next line of the table.
Here is the table and HTML:
<table table id="meter_entries" class="table table-striped table-hover dt-responsive"><thead>
<tr>
<th>ID</th>
<th>Date</th>
<th>Company Code</th>
<th>Oil Height</th>
<th>Water Height</th>
<th>Total Volume</th>
<th>Difference</th>
<th>Actions</th>
</tr>
</thead>
<?php
$s = $stock_automatic; $i=0; $count = count($s);
while($i < $count){
?>
<tr>
<td><?php echo $s[$i]['id']; ?></td>
<td><?php echo $s[$i]['timess']; ?></td>
<td><?php echo $s[$i]['company_code']; ?></td>
<td><?php echo $s[$i]['oil_height']; ?></td>
<td><?php echo $s[$i]['water_height']; ?></td>
<td><?php echo $s[$i]['total_volume']; ?></td>
<td><?php $oldvol = $s[$i-1]['total_volume']; $currvol = $s[$i]['total_volume']; $diff = $oldvol - $currvol; echo $diff; ?></td>
<td>
<span class="fa fa-pencil"></span> Edit
<span class="fa fa-trash"></span> Delete
</td>
</tr>
<?php
$i++;
}
?>
</table>
Here is the output:
It seems that your problem is that you are using datatables and your array $s is actually ordered the opposite way than it is displayed in your example.
To get the correct values you should change your code to the following
<?php if ($i==$count-1){
echo '0';
}
else {
$oldvol = $s[$i+1]['total_volume']; $currvol = $s[$i]['total_volume'];
$diff = $currvol - $oldvol; echo $diff;
} ?>
I want to stop the continous fines in my codes after I click it and save it in database my problem is because if i click the button it's only save in the return.php but the counting is continuous every day like in the picture, I already try it but it didn't work
view_borrow.php
<div class="container">
<form method="POST" action="return_save.php">
<div class="margin-top">
<div class="row">
<div class="span12">
<div class="alert alert-info"><strong>Borrowed Books</strong></div>
<table cellpadding="0" cellspacing="0" border="0" class="table" id="example">
<thead>
<tr>
<th>Book title</th>
<th>Borrower</th>
<th>Type</th>
<th>Date Borrow</th>
<th>Due Date</th>
<th>Date Returned</th>
<th>Fines</th>
<th>Borrow Status</th>
</tr>
</thead>
<tbody>
<?php $user_query=mysqli_query($dbcon,"select * from borrow
LEFT JOIN member ON borrow.member_id = member.member_id
LEFT JOIN borrowdetails ON borrow.borrow_id = borrowdetails.borrow_id
LEFT JOIN book on borrowdetails.book_id = book.book_id
ORDER BY borrow.borrow_id DESC
")or die(mysqli_error());
while($row=mysqli_fetch_array($user_query)){
$currentdate = date('Y/m/d');
$start = new DateTime($returndate=$row['due_date']);
$end = new DateTime($currentdate);
$fines =0;
if(strtotime($currentdate) > strtotime($returndate)){
$days= $start->diff($end, true)->days;
$fines = $days > 0 ? intval(floor($days)) * 15 : 0;
$fi = $row['borrow_details_id'];
mysqli_query($dbcon,"update borrowdetails set fines='$fines' where borrow_details_id = '$fi'");
}
$id=$row['borrow_id'];
$book_id=$row['book_id'];
$borrow_details_id=$row['borrow_details_id'];
?>
<tr class="del<?php echo $id ?>">
<td class="test"><?php echo $row['book_title']; ?></td>
<td class="test"><?php echo $row['firstname']." ".$row['lastname']; ?></td>
<td class="test"><?php echo $row['type']; ?></td>
<td class="test"><?php echo $row['date_borrow']; ?></td>
<td class="test"><?php echo $row['due_date']; ?> </td>
<td class="test"><?php echo $row['date_return']; ?> </td>
<td class="test"><?php echo "₱ ".$fines; ?></td>
<td class="test"><?php echo $row['borrow_status'];?></td>
<td > <a rel="tooltip" title="Return" id="<?php echo $borrow_details_id; ?>"
href="#delete_book<?php echo $borrow_details_id; ?>" data-toggle="modal"
class="btn btn-success"><i class="icon-check icon-large"></i>Return</a>
<?php include('modal_return.php'); ?>
<td></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
</div>
</div>
</form>
</div>
this is the modal where I click the return button
modal_return.php
<div id="delete_book<?php echo $borrow_details_id; ?>" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-body">
<div class="alert alert-success">Do you want to Return this Book?</div>
</div>
<div class="modal-footer">
<a class="btn btn-success" href="return_save.php<?php echo '?id='.$id; ?>&<?php echo 'book_id='.$book_id; ?>">Yes</a>
<button class="btn" data-dismiss="modal" aria-hidden="true"><i class="icon-remove icon-large"></i> Close</button>
</div>
</div>
This is the table
You're doing a date diff between $start and $end ($currentdate) but never using $returndate. PHP 7 has a null coalesce operator ?? that would be handy for your scenario (otherwise just use a normal if/else or its ternary equivalent ?:).
Your fees will always be from borrowdate to currentdate with your existing code. The code below should fix your issue.
$borrowdate = new Datetime($row['due_date']);
$currentdate = new Datetime();
$returndate = new Datetime($row['date_return']);
// PHP >= 7
$days = $borrowdate->diff($returndate ?? $currentdate, true)->days;
// PHP < 7
$days = $borrowdate->diff($returndate ?: $currentdate, true)->days;
Im trying to highlight cells that are returning '0000-00-00', empty or invalid date from table but I cant find any solution.Please see the code below
<table class="main_table_med">
<tr class="med_tr">
<th>Exam Location</th>
<th>Exam Date</th>
</tr>
<tr>
<?php
while($row = mysql_fetch_array($select)){
?>
<td id='ex_date' class="tb_dt"><?php echo $row['exam_date']?></td>
<td id='due' class="tb_dt"><?php echo $row['exam_due']?></td>
</tr>
<?php
}
?>
</table>
You tagged the question with javascript jquery but it's really should be PHP question, use PHP to check if it's valid date or not and show the result accordingly:
<?php
function validateDate($date, $formatExpected = 'Y-m-d H:i:s')
{
$d = DateTime::createFromFormat($formatExpected, $date);
return $d && $d->format($formatExpected) == $date
? $date
: 'anyThing';
}
?>
<table class="main_table_med">
<tr class="med_tr">
<th>Exam Location</th>
<th>Exam Date</th>
</tr>
<tr>
<?php
while($row = mysql_fetch_array($select)){
?>
<td id='ex_date' class="tb_dt"><?php echo validateDate($row['exam_date']) ?></td>
<td id='due' class="tb_dt"><?php echo $row['exam_due']?></td>
</tr>
<?php
}
?>
</table>
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>
I am already using a daily report of the income of the store, but I need my report to do the following: i want to see the income report of specific range of dates, or weeks, or months.... essentially that i can choose which days or range of days i want see so i can print my selection.
here is inc.php
<?
$dbtype = "mysql";
$dbhost = "localhost";
$dbname = "anillos";
$dbuser = "rubi";
$dbpass = "----";
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
$conn->exec("set names utf8");
$conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
?>
here is my code:
<table class="table-bordered table-striped table-condensed">
<thead>
<tr>
<th>Fecha</th>
<th>Total Anillos Vendidos</th>
<th>Total ganado del día</th>
</tr>
</thead>
<tbody id="result_fechas">
<tr>
<?
include 'inc.php';
$sql = $conn->prepare("SELECT DATE(start) AS date, COUNT(id_anillos) AS total_anillos, SUM(ventas) AS total_diario_ganado
FROM PRODUCTOS WHERE start >= CURDATE() AND start < CURDATE() + INTERVAL 1 DAY ORDER BY start ASC");
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
?>
<td class="center"><? echo $row['date']; ?></td>
<td class="center"><? echo $row['total_anillos']; ?></td>
<td class="center"><? echo $row['total_diario_ganado']; ?></td>
</tr> <? } ?>
</tbody>
can you help me with this..I really not have a clue...
----UPDATE-----
# Chris78 I made this form:
<form name="fechas" id="fechas" method="post" >
<fieldset>
<legend>Reporte desde : </legend>
<input type="text" value="" placeholder="YYYY-MM-DD" name="f_desde" id="datepicker"/>
<legend>Reporte hasta : </legend>
<input type="text" value="" placeholder="YYYY-MM-DD" name="f_hasta" id="datepicker2" />
<button class="btn btn-inverse" type="submit" name="enviar" >
<i class="icon icon-print icon-white"></i>
Ver Reporte </button>
</fieldset>
</form>
the new SELECT CODE:
<?
$sql = $conn->prepare("SELECT DATE(start) AS date, COUNT(id_anillos) AS total_anillos, SUM(ventas) AS total_ganado FROM PRODUCTOS WHERE start BETWEEN 'f_desde' AND 'f_hasta' ORDER BY start ASC");
$sql->execute();
while($row = $sql->fetch(PDO::FETCH_ASSOC)) {
?>
the table :
<table class="table-bordered table-striped table-condensed">
<thead>
<tr>
<th>rango de fechas seleccionado</th>
<th>Total Anillos Vendidos</th>
<th>Total ganado</th>
</tr>
</thead>
<tbody id="result_fechas">
<tr>
<?
////////SELECT CODE HERE
?>
<td class="center"><? echo $row['date']; ?></td>
<td class="center"><? echo $row['total_anillos']; ?></td>
<td class="center"><? echo $row['total_ganado']; ?></td>
</tr> <? } ?>
</tbody>
and I am try to show the result with ajax call in the same page
<script type="text/javascript">
$(function(){
$("#fechas").submit(function(){
$.ajax({
type:"POST",
url:".reportes.php?ts=" + new Date().getTime(),
dataType:"text",
data:$(this).serialize(),
beforeSend:function(){
$("#loading").show();
},
success:function(response){
$("#result_fechas").append(response);
$("#loading").hide();
}
})
return false;
});
});
});
</script>
reportes.php is the same page where is the form and the table which is waiting the result....but I don´t know where is the error, because not catch ajax the data and the page is refreshing when I clic the button...can you help me with this.
If you are wanting to run a query with a date range you can use the BETWEEN statement.
$sql = $conn->prepare("SELECT DATE(start) AS date, COUNT(id_anillos) AS total_anillos, SUM(ventas) AS total_diario_ganado
FROM PRODUCTOS WHERE start BETWEEN 'Older Date Here' AND 'Newer Date Here' ORDER BY start ASC");
You create a form with those two inputs that you talked about, when you retrieve the input value you give the treatment "date('Y-m-d', inputvalue)" if it isn't already treated. Then you replace "CURDATE()" on you sql statement for the treated inputvalues.
Dates should always be given in the "yyyy-mm-dd" format to MySql.
Source: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-types.html