I'm working on a site where workers' hours are calculated based on when they clock in (start) and clock out (end). end - start = total. I have successfully computed the difference between the start and end. The start, end, and total are inserted into a DB. The "Shift total" comes from the SQL SUM of total in the db. The last foreach statement is the code in question. I want to format the "Shift total" as 00:00:00 so that it would show as 00:01:26 instead of 126. I have tried ". $row['SUM(total)'] + '00:00:00' . " but it didn't work. Any suggestions on how I should go about this? Thanks in advance. I pasted the results first then the code.
____start____________end____________total
10:44:46 AM 10:44:49 AM 00:00:03
10:50:12 AM 10:50:14 AM 00:00:02
10:50:27 AM 10:50:32 AM 00:00:05
11:12:02 AM 11:13:18 AM 00:01:16
Shift total --> 126
$sql3 = "SELECT date, startTime, endTime, total FROM timeSheet WHERE userName='$user_name'";
$sql4 = "SELECT SUM(total) FROM timeSheet WHERE userName='$user_name'";
echo "<table border='1'>
<tr>
<th>date</th>
<th>start</th>
<th>end</th>
<th>total</th>
</tr>";
foreach ($conn->query($sql3) as $row) {
$start = explode (" ", $row['startTime']);
$end = explode(" ", $row['endTime']);
if ($start[1] == '00:00:00'){
$start[1] = '0';
}else if ($end[1] == '00:00:00'){
$end[1] = '0';
}else{
$start[1] = date("h:i:s A", strtotime($start[1]));
$end[1] = date("h:i:s A", strtotime($end[1]));
}
echo "<tr>
<th>" . $row['date'] . "</th>
<th>" . $start[1] . "</th>
<th>" . $end[1] . "</th>
<th>" . $row['total'] . "</th>
</tr>";
}
foreach ($conn->query($sql4) as $row) {
echo "<tr>
<th></th>
<th></th>
<th>SUM</th>
<th>". $row['SUM(total)'] . "</th>
</tr>";
}
echo "</table>";
you can do it with the TIME function:
$sql4 = "SELECT TIME(SUM(total)) AS totalSum FROM timeSheet WHERE userName='$user_name'";
//....
foreach ($conn->query($sql4) as $row) {
echo "<tr>
<th></th>
<th></th>
<th>SUM</th>
<th>". $row['totalSum'] . "</th>
</tr>";
}
hint:
actually you don't need the total column in your database. You can use i.e
SELECT date, startTime, endTime, TIMEDIFF(startTime, endTime) AS total FROM timeSheet WHERE userName='$user_name'"
and
SELECT TIME(SUM(TIMEDIFF(startTime, endTime))) AS totalSum FROM timeSheet WHERE userName='$user_name'
for further information have a look at: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_timediff
Related
Hope everyone is awesome. What is the best way to display only future events in an event list which is generated by PHP and SQL.
function select_events($sql) {
include 'connect.php';
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table id='event-list'>\n
<tr>\n
<th id='event'>Event</th>\n
<th id='date-of-event'>Date</th>\n
<th id='time'>Time</th>\n
<th id='place'>Place</th>\n
<th id='admission'>Admission</th>\n
</tr>";
}
while ($row = $result->fetch_assoc()) {
$date = new DateTime($row['date']);
$formatted_date = $date->format('d/m/Y');
$time = new DateTime($row['time']);
$formatted_time = $time->format('HH:MM');
echo "<tr>\n
<td id='event'>".$row['event']."</td>\n
<td id='date-of-event'>".$formatted_date."</td>\n
<td id='time'>".$row['time']."</td>\n
<td id='place'>".$row['place']."</td>\n
<td id='admission'>".$row['admissions']."</td>\n
</tr>\n";
}
echo "</table>\n";
}
<?php select_events("SElECT * FROM events ORDER BY date, time"); ?>
Thanks everyone in advance. :)
try this,
<?php select_events("SElECT * FROM events WHERE `date` >= CURDATE() ORDER BY `date`, `time`"); ?>
use backticks for mysql reserved keywords
You need to add a condition to check for date and also for time.
Check whether date is greater than today and time is greater than current time.
<?php select_events("SElECT * FROM events WHERE `date` >= " . date('Y-m-d') . " AND `time` > " . date('H:i:s') . " ORDER BY `date`, time"); ?>
Hi in the below i want to show Consultation Charges values for that i took the td.But in that td not displying anything even td also not showing.
After executing this query i want to find the no. of rows based on the rows i want to display the data.
My expected output:
Bill Particular Bill Sub Particular Doctor Date Dis. Amt.
Consultation Charges:
all the values based on no of rows.
php
<table width="100%">
<th>Bill Particular</th>
<th>Bill Sub Particular</th>
<th>Doctor</th>
<th>Date</th>
<th>Dis. Amt.</th>
<th>Charge</th>
<th>No. of Times</th>
<th>Amount</th>
</table>
<tr><th colspan=2>Consultation Charges:</th>
<?php
$div_options = array();
$sql = "SELECT ibp.ipd_bp_id, ibp.bp_id, bp.bp_name, ibp.bsp_id, bsp.bsp_name, ibp.doctor_id, ab.employee_name doctor, ibp.date date, ibp.amount charge, ibp.discount_amount discount, ibp.no_of_time, (ibp.no_of_time * ibp.amount) total_amount
FROM bill_particular_master bp
INNER JOIN ipd_bill_particular ibp ON ibp.bp_id = bp.bp_id
LEFT OUTER JOIN bill_sub_particular bsp ON bsp.bsp_id = ibp.bsp_id
LEFT OUTER JOIN address_book ab ON ab.employee_id = ibp.doctor_id
WHERE ibp.ipd_reg_no = '$ipd_no'
AND bsp.consultant =1
AND bsp.package = 0
AND bsp.admission = 0
AND bp.bp_name != 'Scan Charges'
AND bp.bp_name !='Procedure'";
$sth = $dbh->query($sql);
//$row=$dbh->fetch();
$i=1;
while($row=$sth->fetch(PDO::FETCH_ASSOC)){
$sub_arr['bp_name'] = $row['bp_name'];
$sub_arr['bsp_name'] = $row['bsp_name'];
echo "<tr>
<td>Here is the text - " . $sub_arr['bp_name'] . "</td>
<td>The ID of the text is - " .$sub_arr['bsp_name'] . "</td>";
if($i !== 0) {
echo "<td>The ID of the previous entry is - " .$sub_arr['bp_name'] . "</td>";
}
else {
echo "<td> </td>";
}
echo "</tr>";
$i = $row['bp_name'];
}
?>
</tr>
echo "<td>The ID of the previous entry is - " . $row['bp_name'] . "</td>";
You have there variable thats not even defined in your code.
<?php require_once('../includes/header.php');?>
<?php require_once('../includes/connection.php');?>
<?php include('../includes/get_username.php');?>
<?php
include('sanitise.php');
$month = sanitise($_GET['month']);
?>
<table id="contentbox">
<tr>
<td>
<?php
$color="1";
//view record
$qry = mysql_query("SELECT * FROM tbl_dv WHERE monthname(date_added) = '$month' ORDER BY dv_id DESC");
echo "<table class='dvr_table' id='alternatecolor' width='100%'>
<tr>
<th>DATE ADDED</th>
<th>CT</th>
<th>PAYEE</th>
<th>PARTICULAR</th>
<th>PM</th>
<th>VOUCHER NO.</th>
<th>NET</th>
<th>OBR NO.</th>
<th>";
//RESPO/Office
$sql = "SELECT respo FROM tbl_dv WHERE monthname(date_added) = '$month' GROUP BY respo";
$result = mysql_query($sql);
echo "<select name='respo'>
<option value=' ' disabled='disabled' selected='selected'>Select a RESPO/Office</option>";
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['respo'] . "'>" . $row['respo'] . "</option>";
}
echo "</th>
<th>ACCOUNT CODE</th>
<th>FPP CODE</th>
<th>DEDUCTION</th>
</tr>";
while ($row = mysql_fetch_array($qry)) {
$dv_id = $row['dv_id'];
if($color==1){
echo "<tr bgcolor='#ffffff'>
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc.......
$color="2";
} else {
echo "<tr bgcolor='#ebeaea'>
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc......
$color="1";
}
}
echo "</tr>";
?>
I Have all it work. Only 1 PROBLEM.. I CANNOT DISPLAY the selected RESPO/OFFICE. As I select the RESPO/OFFICE it will sort out the selected RESPO/OFFICE... What should I resvise my code or add code.. Please Help!
A few suggestions first:
mysql is depreciated and you should move to PDO prepared statements
or mysqli. You can consult the php manual for this.
comment your code, you are not able to follow along unless you read
from top to bottom.
Now onto your code:
<?php
// Start document with includes needed, header, connection, functions.
require_once('../includes/header.php');
require_once('../includes/connection.php');
include('../includes/get_username.php');
include('sanitise.php');
// Start with the month variable posted from $_GET, this will begin the initial display.
$month = sanitise($_GET['month']);
?>
<!-- Begin Table Display and Layout -->
<table id="contentbox">
<tr>
<td>
<?php // Open PHP Tag to get variables to display in the table
$color="1";
// Initial Query to get the data for the month passed by GET, ordering by dv_id.
$qry = mysql_query("SELECT * FROM tbl_dv WHERE monthname(date_added) = '$month' ORDER BY dv_id DESC");
// Format the table rows for display
echo "<table class='dvr_table' id='alternatecolor' width='100%'>
<tr>
<th>DATE ADDED</th>
<th>CT</th>
<th>PAYEE</th>
<th>PARTICULAR</th>
<th>PM</th>
<th>VOUCHER NO.</th>
<th>NET</th>
<th>OBR NO.</th>
<th>";
// Fill the SELECT option with all the RESPO/Office data
$sql = "SELECT respo FROM tbl_dv WHERE monthname(date_added) = '$month' GROUP BY respo";
$result = mysql_query($sql);
// Display the select with the newly populated content
echo "<select name='respo'>
<option value=' ' disabled='disabled' selected='selected'>Select a RESPO/Office</option>";
// While the result contains data we will display respo as a select option.
while ($row = mysql_fetch_array($result)) {
echo "<option value='" . $row['respo'] . "'>" . $row['respo'] . "</option>";
}
// Finish the table header
echo "</th>
<th>ACCOUNT CODE</th>
<th>FPP CODE</th>
<th>DEDUCTION</th>
</tr>";
// While we have data from the original query we will display it
while ($row = mysql_fetch_array($qry)) {
$dv_id = $row['dv_id']; // grab the dv_id
if($color==1){ // Setup color scheme
echo "<tr bgcolor='#ffffff'> // Color for the row
// Let's begin filling the table with data
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc.......
$color="2";
} else {
echo "<tr bgcolor='#ebeaea'>
<td style='text-align:center;'>" .date_format(date_create($row['date_added']), 'm/d/y')." </td>
<td style='text-align:center;'>" .$row['cashtype']."</td>
etc......
$color="1";
}
}
echo "</tr>";
?>
After reviewing the above code it is clear that you haven't setup a query to filter the data that you are displaying based on the selected option.
Your first query:
// Initial Query to get the data for the month passed by GET, ordering by dv_id.
$qry = mysql_query("SELECT * FROM tbl_dv WHERE monthname(date_added) = '$month' ORDER BY dv_id DESC");
You could append a new variable for the selected option, expand your WHERE statement. WHERE monthname(date_added) = '$month' && respo = '$respo' and move this query after the select option so it has access to the value chosen by the select option. Lastly, unless you are going to use jQuery to deal with the select box changing values, you would need a submit button to show the state change for the select option.
Pseudo Code:
Check for Submit
Change value of the department in the query WHERE clause
Display the requested data
This should get you in the direction you need to go in order to achieve the desired result.
EDIT:
In the 3rd answer you posted:
$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($respo)."' AND year(date_added)='$year' GROUP BY date_added ";
You said it is not filtering by the month being passed in, you are not adding it into your query, specifically the WHERE clause. So it should be:
$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($respo)."' AND month(month_added)='$month' AND year(date_added)='$year' GROUP BY date_added ";
FINAL EDIT:
Your first issue stems from not sending the $_GET['date_added'] value to the view page, so you will never display anything. Properly format the select box and add the data for the RESP and Date data from the database:
echo "<option value='". $row['respo'] . "+". $row['date_added'] . "'>" . $row['respo'] . " (".$row['count(*)'].")</option>";
Now that we are passing the respo and the date string we can work with the data on the next page:
/* UNCOMMENT NEXT LINE FOR ERROR DETECTION */
//error_reporting( E_ALL );
/* GET THE ENTIRE STRING PASSED FROM THE SELECT OPTION */
$respo = $_GET['respo'];
/* EXPLODE THE DATA BY + SYMBOL TO GET THE DATA */
$data = explode("+", $respo);
/* FORMAT THE DATETIME FROM THE STRING TO A FRIENDLY FORMAT */
$month = date("m", strtotime($data[1])) . "<br />";
$year = date("Y", strtotime($data[1])) . "<br />";
This is how I choose to work with the data and get what I needed, so now the query would be:
$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($data[0])."' && year(date_added)='$year' && month(date_added)='$month' ";
This will only display the records for the RESPO and the date by year and month passed into the page via $_GET.
<?php require_once('../includes/header.php');?>
<?php
$respo = $_GET['respo'];
$month = (int) (!empty($_GET['date_added']) ? $_GET['date_added'] : date('m'));
$year = (int) (!empty($_GET['date_added']) ? $_GET['date_added'] : date('Y'));
//database call here
$viewrecord = "SELECT * FROM tbl_dv WHERE respo='".mysql_real_escape_string($respo)."' AND year(date_added)='$year' GROUP BY date_added ";
$result = mysql_query($viewrecord, $db) or die (mysql_error());
$num_result = mysql_num_rows($result);
{
echo "<table border='1' width='100%' style='border:1px solid silver' cellpadding='5px' cellspacing='0px'>
<tr bgcolor='#666666' style='color:#FFFFFF'>
<th>Date Encoded</th>
etc...... (header here)
while ($row = mysql_fetch_row($result)) {
echo '<tr>';
echo "<tr ><td align='center'>" .date_format(date_create($row[17]), "m/d/y")."</td>
etc...
}
mysql_close($db);
?>
This is the code i come up. This is just an alternative solution. But the problem with this solution is that. I won't display the record in MONTH as call. THe respo is working as I select the RESPO it will display the all RESPO I ask, but the problem is that it include all the previous months with the RESPO I select. When I just select the specific month only..
//RESPO/Office
**echo '<form action="view.php" method="post">';**
$byrespo = mysql_query("SELECT DISTINCT count(*), respo FROM tbl_dv WHERE monthname(date_added)='$month' GROUP BY respo");
echo "<select name='respo' onchange='this.form.submit()'>
<option value=' ' disabled='disabled' selected='selected'>Select a RESPO/Office</option>";
while ($row = mysql_fetch_array($byrespo)) {
echo "<option value='" . $row['respo'] . "'>" . $row['respo'] . " (".$row['count(*)'].")</option>";
}
echo "</select></form>";
I just rushing to came up with solution.. So I add a and I create another view.php
I'm trying to display data starting at a certain time and day and ending and another time and day. here is my code:
<?php
include 'includes/connect3.php';
$query = "SELECT * FROM u_visits WHERE date >= '2013-08-31 22:56:20' AND date <= '2013-
08- 31 23:59:59'";
$result = mysqli_query($con,$query);
echo "<table><tr>
<th>USER ID</th>
<th>TIMES VISITED</th>
</tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['visits'] . "</td></tr>";
}
echo "</table>";
?>
When I go to the page, only the table header is displayed with no data showing.
you can also use BETWEEN for range comparisons.
$query = "SELECT * FROM u_visits WHERE `date` BETWEEN '2013-08-31 22:56:20' AND '2013-08-31 23:59:59'";
The reason you didn't see any result is because your query has some errors. The error in your query is that you have a space after 2013-08- in your where clause (date <= '2013-08- 31 23:59:59'";)
Had you used proper error handling, you would have noticed this yourself. One common way to do this is:
$result = mysqli_query($con,$query) or die ('Query execution failed: ' . mysqli_error($con));
I have devised the following code; to calculate the waiting time and expected time for a patient. The code should also echo a warning if the patient has been waiting too long.
Please note: Waiting_time is DATETIME in the database.
Here is the code;
<?php
$conn = mysqli_connect("localhost","root","") or die ("No connection");
mysqli_select_db($conn, "a&e") or die('Could not select database.');
$query = "SELECT PatientID, Forename, Surname, Gender, Illness, Priority, Arrival_time,NOW() as now,ABS(TIMEDIFF(NOW(), Arrival_time)) as Waiting_Time FROM Patient";
$result = mysqli_query($conn, $query) or die("Invalid query");
echo "<table border='1'>
<tr>
<th>PatientID</th>
<th>Forename</th>
<th>Surname</th>
<th>Gender</th>
<th>Illness</th>
<th>Priority</th>
<th>Waiting Time</th>
</tr>";
while ($row = $result->fetch_object()){
//Select the expected and discharge time for this patient.
$query2 = "SELECT Abs(TIMEDIFF(Expected_Time,'00:00:00')) as Expected,Abs(TIMEDIFF(Discharge_Time,'00:00:00')) as Discharge ".
"FROM priority_time ".
"WHERE Illness = '".$row->Illness."'".
" AND Priority = '".$row->Priority."'".
";";
$result2 = mysqli_query($conn, $query2) or die("Invalid statement: ".$query2);
$row2 = $result2->fetch_object();
$expected = $row2->Expected;
$discharge = $row2->Discharge;
echo "expected-> ".$expected." discharge-> ".$discharge;
if($expected > $discharge){
echo "There is a problem with the database consistency, expectedTime must be less than dischargeTime!";
}
//Set the patient color.
if($row->Waiting_Time < $expected && $row->Waiting_Time < $discharge){
echo "<tr>";
}
if($row->Waiting_Time >= $expected && $row->Waiting_Time < $discharge){
echo '<tr bgcolor="#FFFF00">';
}
if($row->Waiting_Time > $expected && $row->Waiting_Time > $discharge){
echo '<tr bgcolor="#FF0000">';
}
//Print patient info
echo
"<td>" . $row->PatientID . "</td>
<td>" . $row->Forename . "</td>
<td>" . $row->Surname . "</td>
<td>" . $row->Gender . "</td>
<td>" . $row->Illness . "</td>
<td>" . $row->Priority . "</td>
<td>" . $row->Waiting_Time . "(".$expected."-".$discharge.") </td>";
//Close row
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
EDIT:
On each row, in the column Waiting Time it is showing the waiting time in seconds, and in brackets the currentTime a minus and the arrival time, just for checking. How do I convert the waiting time to the format hh:mm:ss to have a better representation for the user?
Showing;
Waiting time
01:15:42(10500.000000-10500.000000)
Why is it displaying (10500.000000-10500.000000)?
Try this:
gmdate("H:i:s", $seconds)
If that doesn't work, have a look at How to convert seconds to time format?
Update:
To do this in the SQL statement try something like this:
SELECT TIME_TO_SEC(TIMEDIFF('2013-03-27 12:00:00', '2013-03-27 10:00:00')) diff;
So something like this:
SELECT PatientID
, Forename
, Surname
, Gender
, Illness
, Priority
, Arrival_time
, NOW() as now
, TIME_TO_SEC(TIMEDIFF(NOW(), Arrival_time)) as Waiting_Time
FROM Patient;
Then you would change this line:
<td>" . $row->Waiting_Time . "(".$expected."-".$discharge.") </td>
to this:
<td>" . gmdate("H:i:s", $row->Waiting_Time) . "(".$expected."-".$discharge.") </td>
When dealing with dates and times in php strtotime() is your biggest friend. Lucky the date() function is all you need for this. Something that is going to convert seconds to a readable format could be the following.
$time = strtotime('now');
echo $time . '<br />';
$var = date('H:i:s',$time);
echo $var;