How to find out missing date in MySQL using PHP?
echo "<tr> <th>Username</th><th>Date</th><th>Check In</th><th>Check Out</th> </tr>";
// get results1 from database
$result1 = mysql_query("SELECT * FROM attend WHERE user_name='ali' AND date BETWEEN '2015-07-01' AND '2015-07-15' order by date");
while($row = mysql_fetch_array($result1))
{
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['user_name'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['checkin'] . '</td>';
echo '<td>' . $row['checkout'] . '</td>';
echo "</tr>";
}
echo "</table>";
currently result
Username Date Check In Check Out
ali 2015-07-01 11:30:34 17:23:47
ali 2015-07-02 10:11:34 17:15:15
ali 2015-07-03 09:32:34 18:16:27
ali 2015-07-06 10:41:34 16:56:13
ali 2015-07-07 08:51:34 17:36:01
ali 2015-07-08 05:61:34 17:16:26
ali 2015-07-09 04:11:34 17:14:12
ali 2015-07-10 02:81:34 17:25:25
ali 2015-07-13 11:71:34 17:02:29
ali 2015-07-14 10:81:34 17:04:20
ali 2015-07-15 09:31:34 17:00:43
and i want result like this
Username Date Check In Check Out
ali 2015-07-01 11:30:34 17:23:47
ali 2015-07-02 10:11:34 17:15:15
ali 2015-07-03 09:32:34 18:16:27
2015-07-04
2015-07-05
ali 2015-07-06 10:41:34 16:56:13
ali 2015-07-07 08:51:34 17:36:01
ali 2015-07-08 05:61:34 17:16:26
ali 2015-07-09 04:11:34 17:14:12
ali 2015-07-10 02:81:34 17:25:25
2015-07-11
2015-07-12
Ali 2015-07-13 11:71:34 17:02:29
ali 2015-07-14 10:81:34 17:04:20
ali 2015-07-15 09:31:34 17:00:43
All of your missing check ins are not assigned to any user. If you want to fetch records assigned to user ali or unassigned, between gives dates:
SELECT * FROM attend WHERE (user_name='ali' OR user_name = '') AND date BETWEEN '2015-07-01' AND '2015-07-15' order by date
It would be much cleaner if you show us your schema.
I'd use timestamp for that date column. If you do this, you can simply "check" which day is expected to come next. If its not that day, print that day like you wanted and then continue, if you got that expected date.
Do do so, you should save all your timestamps at a given time of that day. Like always 12:00:00. This makes handling those numbers much easier.
echo "<tr> <th>Username</th><th>Date</th><th>Check In</th><th>Check Out</th> </tr>";
// get results1 from database
$iLastTimestamp = 0
$result1 = mysql_query("SELECT * FROM attend WHERE user_name='ali' AND date BETWEEN '2015-07-01' AND '2015-07-15' order by date");
while($row = mysql_fetch_array($result1))
{
if ($iLastTimestamp != 0)
{
while ($iLastTimestamp != $row['date'])
{
// echo out the contents of each row into a table
echo "<tr>";
echo '<td> </td>';
echo '<td>' . date("Y-m-d", $iLastTimestamp) . '</td>';
echo '<td> </td>';
echo '<td> </td>';
echo "</tr>";
$iLastTimestamp = strtotime("+1 Day 12:00:00", $iLastTimestamp)
}
}
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['user_name'] . '</td>';
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['checkin'] . '</td>';
echo '<td>' . $row['checkout'] . '</td>';
echo "</tr>";
}
echo "</table>";
Another way is to convert your date into an timestamp first, before using the code I posted. You can use the "strtotime"-Func here, too. Just add the time, just to be sure. :)
You can do this with a range in a function, this will permit you to reuse your code:
function getAttendInInterval($begin,$end) {
$begin = new DateTime( $begin );
$end = new DateTime( $end );
$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($begin, $interval, $end);
foreach ( $period as $dt ) {
$result = mysql_query('SELECT * FROM attend WHERE user_name="ali" AND date = '".$dt->format( 'Y-m-d' )."' order by date');
...
}
This fonction permits to show the attend for each day in an interval.
and then:
getAttendInInterval('2015-07-01','2015-07-15');
you can use date in LOOP and if date is match in database then show all info. of database with date otherwise show only date.
Related
<?php
echo "<table class=\"table table-hover table-bordered\">";
echo "<thead>";
echo "<tr>";
$result=mysqli_query($con,"SELECT day FROM time_slot ORDER BY day;");
while($row=mysqli_fetch_array($result))
{
$days = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$day = $row['day']-1;
$day = $days[$day];
echo '<th>';
echo $day;
echo '</th>';
}
echo "</tr>";
echo "</thead>";
require('../includes/connection.php');
$result=mysqli_query($con,"SELECT * FROM time_slot ORDER BY day DESC;");
while($row=mysqli_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
This portion of data should be display vertically in table with respect to Day
//This code to split time into intervals
$starttime = $row['start_time']; // your start time
$endtime = $row['end_time']; // End time
$duration = $row['slot_time'];
$array_of_time = array ();
$start_time = strtotime ($starttime); //change to strtotime
$end_time = strtotime ($endtime); //change to strtotime
$add_mins = $duration * 60;
while ($start_time <= $end_time) // loop between time
{
echo '<td>';
echo "<div align=\"center\">";
print_r(date ("h:i", $start_time) ."-". date("h:i", $start_time += $add_mins));
//
echo "</br><input type=\"radio\" name=\"texi\" value=\"1\" ></input>";
echo '</td>' ;
echo '</div>';
}
echo "</tr>";
echo "</tbody>";
}
mysqli_close($con);
echo "</table>
</div>
<input type=\"submit\" value=\"Book Slot\" class=\"button_drop\">
</form>";
?>
I want to show time slots vertically with respect to day in table.
This is the output of this code:
But I want to display time which is showing horizontally that should be shown vertically. Eg. 1-3, 3-5, 5-7 of first row should be shown below Monday, then next row should be shown below Tuesday and so on.
Try using a query like this:
SELECT day FROM time_slot ORDER BY time_slot, day;
That should get the data in an easier order to process. I can't see your exact data structure but it looks like you should get 7x 01:00-03:00, then 7x 03:00-05:00 etc. Each set should be in order by day. Then you can just write out your <td>s in the order the data comes from the DB. Keep track of when the day value changes (or just count up to 7) and start a new <tr> at that time.
I wanted to add 30 days in my current order_date which is retrieved from db automatically and display the date which is 30 days ahead.But the result which i am getting is something like this 14437368002015-11-01 when the order_date was 2015-10-01.1443736800 gets Added before the date .Please help!
while($row = mysqli_fetch_array($query)){
echo '<tr>'.
'<td align="center">'.$row['order_no'].'</td>'.
'<td align="center">'.$row['order_date'].'</td>'.
'<td align="center">'.$row['store_used'].'</td>'.
'<td align="center">'.$row['status'].'</td>'.
'<td align="center">'.'Rs.'.$row['cashback'].'</td>'.
'<td align="center">'.$date = strtotime($row['order_date']);
$date = strtotime("+30 day", $date);
echo date('Y-m-d', $date);'</td>'.
'</tr>';
}
echo '</tbody></table></form>'."\n\n";
mysqli_close($dbc2);
?>
Results:
Order ID Order Date Store Used Status Cashback Est.Cashback.Date
134 2015-10-02 Flipkart To be Checked Rs. 14437368002015-11-01
It seems you have concatenation issue simply you can also directly use date function like as
'<td align="center">'.date('Y-m-d',strtotime("{$row['order_date']} +30 days")).'</td>'.
It seems from your code that your code is echoing date twice
'<td align="center">'.$date = strtotime($row['order_date']);//starting of td
and
echo date('Y-m-d', $date);'</td>'. //ending of td
This should work for you, you are not converting the date after strtotime().So convert it with date()
$date = strtotime("2015-10-01");
$date = date("Y-m-d",strtotime("+30 day", $date));
echo $date;// outputs 2015-10-31
I have a database with a table of login and logoff times for staff.
Im looking for which staff clocked on or off between Sunday 6am and Thursday 4am or Saturday 6:30am and Saturday 3pm.
I want the end user to be able to select any date range from a web interface (ie 1/1/14 start and 12/1/14 end) and from this work out who worked during the day and time ranges.
I want to be able to display a list of workers and their login and logoff time.
thus far I have worked out how to check if a user has logged by using Determine Whether Two Date Ranges Overlap
But im stumped with the pseudo code for how to work out the daterangeB
so far I have:
start at datefrom,
check if datefrom = dateto, if true end loop,
find out day name,
check if saturday, if saturday add timestamps to array (start and finish),
increment datefrom by 1 day, start loop again.
this will give me an array of the saturday daterangeB's between the dates selected by the user.
However the bigger date range is an issue, because its possible that the first day is say a Tuesday, and if i'm checking for Sundays and Thursday, say i find a Thursday, i won't have a start day as sunday, I will need to make the Tuesday at 00:00:00. And similarly the opposite could happen, i could find a Sunday, and the last day that the user selects could be a Monday and therefore I need to make that the end date for daterangeB
start at datefrom,
check if datefrom = dateto, if true end loop,
find out day name,
check if Sunday, if Sunday add timestamp to array (start),
increment datefrom by 1 day, start loop again.
and
start at datefrom,
check if datefrom = dateto, if true end loop,
find out day name,
check if Thursday, if Thursday add timestamps to array (start and finish),
increment datefrom by 1 day, start loop again.
So for all those that are interested, here is a dumbed down verson of what i ended up doing:
$stcars = array();
// Create connection
try {
$db = new PDO('mysql:host=localhost;dbname=Reports;charset=utf8', 'XXX', 'XXX');
$db->query('hi'); //invalid query!
} catch(PDOException $ex) {
echo "An Error occured!" . $ex->getMessage(); //user friendly message
}
$stmt = $db->prepare('SELECT CarNumber FROM STcars WHERE CarNumber > 0');
if ($stmt->execute()){
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
array_push($stcars, $row[CarNumber]);
}// end while
}// end if
$DateFrom = $_POST["DateFrom"];
$DateTo = $_POST["DateTo"];
$DateToArray = date('m/d/Y', strtotime($DateTo . ' + 1 day'));
$period = new DatePeriod(
new DateTime($DateFrom),
new DateInterval('P1D'),
new DateTime($DateToArray)
);
foreach($period as $date)
{
$daterange[] = $date->format('m/d/Y');
}
// MS SQL Server with PDO_DBLIB
$DBH = new PDO("dblib:host=$myServer;dbname=$myDB", $myUser, $myPass);
for ($i = 0; $i < count($stcars); ++$i) {
$CarNumber = $stcars[$i];
$STH3 = $DBH->query("SELECT dbo.Driver.DriverNumber, dbo.Driver.DriverName, dbo.DriverLogon.LogonTime, dbo.DriverLogon.LogoffTime, dbo.Vehicle.RegistrationNumber, dbo.OwnerOperator.OOName FROM dbo.Driver INNER JOIN dbo.DriverLogon ON dbo.Driver.DriverID = dbo.DriverLogon.DriverID INNER JOIN dbo.Vehicle ON dbo.DriverLogon.VehicleID = dbo.Vehicle.VehicleID INNER JOIN dbo.OwnerOperator ON dbo.Vehicle.OperatorID = dbo.OwnerOperator.OwnerOperatorID WHERE dbo.Vehicle.CarNumber = '$CarNumber' AND dbo.DriverLogon.LogonTime >= '$DateFrom $Beginning' AND dbo.DriverLogon.LogoffTime <= '$DateTo $End' AND dbo.Driver.DriverNumber != '7777' AND dbo.Driver.DriverNumber != '83890';");
$STH3->setFetchMode(PDO::FETCH_OBJ);
while($row3 = $STH3->fetch()) {
$logonday = date("D", strtotime($row3->LogonTime));
$chkStartTime = strtotime($row3->LogonTime);
$chkEndTime = strtotime($row3->LogoffTime);
$Tplate = $row3->RegistrationNumber;
$Operator = $row3->OOName;
$logontime = gmdate("D d/m/Y H:i:s", strtotime($row3->LogonTime));
$logofftime = gmdate("D d/m/Y H:i:s", strtotime($row3->LogoffTime));
for ($e = 0; $e < count($daterange); ++$e) {
$Date = $daterange[$e];
$day = date("D", strtotime($Date));
if(strpos( $day, $Mon ) !== FALSE){
$startTime = strtotime($Date . " " . $Beginning);
$endTime = strtotime($Date . " " . $End);
if($chkStartTime >= $endTime || $chkEndTime <= $startTime){}else
{
echo "<tr>" ;
echo "<td>" . $day . "</td> ";
echo "<td>" . $row3->DriverNumber . "</td> ";
echo "<td>" . $row3->DriverName . "</td> ";
echo "<td>" . $Operator . "</td> ";
echo "<td>" . $CarNumber . "</td> ";
echo "<td>" . $Tplate . "</td> ";
echo "<td>" . $logontime . "</td> ";
echo "<td>" . $logofftime . "</td> ";
//echo "<td>" . "Logon after Time End or Logoff before Time Start" . "</td> ";
//echo "<td>" . gmdate("D d/m/Y G:i:s", strtotime($Date . " " . $Beginning)) . "</td> ";
//echo "<td>" . gmdate("D d/m/Y G:i:s", strtotime($Date . " " . $End)) . "</td> ";
echo "</tr>" ;
$list1 ="$day, $row3->DriverNumber, $row3->DriverName, $Operator, $CarNumber, $Tplate, $logontime, $logofftime " . "\n";
fwrite($handle, $list1);
}
May I know how to I implement a SUM amount on my current sql code?
I tried Google around but it doesn't seems to have the answer I want. Most of them are MySQL format which is totally different with my current code where it is using PDO library.
My code:
try {
$start_date = "3/1/2014";
$end_date = "4/1/2014";
$start = DateTime::createFromFormat('d/m/Y', $start_date);
$end = DateTime::createFromFormat('d/m/Y', $end_date);
$query = $db->prepare("SELECT * FROM log WHERE DATE(log_datetime) >= :start AND DATE(log_datetime) < :end + INTERVAL 1 DAY");
$success = $query->execute(array(':start'=>$start->format('Y-m-d'), ':end'=>$end->format('Y-m-d')));
}
$rows = $query->fetchAll(PDO::FETCH_OBJ);
//result
foreach ($rows as $row) {
$log_id = $row->log_id;
$dob = $row->dob;
$log_datetime = $row->log_datetime;
$log_count = $row->log_count;
$amount = $row->amount;
echo '<tr>';
echo '<td>' . htmlspecialchars($log_id) . '</td>';
echo '<td>' . htmlspecialchars($dob) . '</td>';
echo '<td>' . htmlspecialchars($log_datetime) . '</td>';
echo '<td>' . htmlspecialchars($log_count) . '</td>';
echo '<td>' . htmlspecialchars($amount) . '</td>';
echo '</tr>';
}
sqlfiddle: http://sqlfiddle.com/#!2/6fa8f/5
I want to sum 'log_count' and return the total of people visit on the date that user input and sum amount and return the value of earning on that specific date.
How do I do it in PHP PDO?
Any help is really appreciated!
UPDATE: You can just calculate your sums while you iterate over the resultset. Something like this
echo '<table><tbody>';
$log_count_total = 0;
$amount_total = 0;
foreach ($rows as $row) {
$log_count_total += $row->log_count;
$amount_total += $row->amount;
echo '<tr>';
echo '<td>' . $row->log_id . '</td>';
echo '<td>' . $row->dob . '</td>';
echo '<td>' . $row->log_datetime . '</td>';
echo '<td>' . $row->log_count . '</td>';
echo '<td>' . $row->amount . '</td>';
echo '</tr>';
}
echo '<tr>';
echo '<td></td><td></td><td></td>';
echo '<td><strong>' . $log_count_total . '</strong></td>';
echo '<td><strong>' . $amount_total . '</strong></td>';
echo '</tr>';
echo '</tbody></table>';
Output:
4 1956-05-15 2014-01-03 20:37:27 1 10
5 1940-07-31 2014-01-03 21:37:27 1 10
6 2000-02-18 2014-01-04 18:30:27 1 10
7 1955-08-31 2014-01-04 19:32:27 1 10
8 1983-11-22 2014-01-04 20:31:27 1 10
9 1958-08-09 2014-01-04 21:05:27 1 10
6 60
If you need to get totals in one go
SELECT SUM(log_count) total_log_count, SUM(amount) total_amount
FROM log
WHERE log_datetime >= '2014-01-03'
AND log_datetime < '2014-01-04' + INTERVAL 1 DAY
BTW: Don't apply DATE() function to log_datetime in WHERE clause. Not only it's useless in this case but also invalidates the usage of any index you may have on that column effectively causing a full scan.
Output:
| TOTAL_LOG_COUNT | TOTAL_AMOUNT |
|-----------------|--------------|
| 6 | 60 |
Here is SQLFiddle demo
I have in PHP:
<?php
$columns = array('mon','thu','wed','tue', 'fri', 'sat', 'sun');
$num_cols = count($columns);
$col = 0;
$datetime = new DateTime("06:00");
echo "<table>"; // border=\"1\" for visible border
// Day header
echo "<tr>";
foreach($columns as $col) {
echo "<td at='$col'>$col</td>";
}
echo "</tr>";
// Times
for($i=0; $i<20; $i++) { // FIXME for more entries or other step width
echo "<tr>";
$datetime->modify('+30 minutes');
for ($j=0; $j<$num_cols; $j++)
echo '<td class="' . $datetime->format('H:i') . '">' . $datetime->format('H:i') . '</td>';
echo "</tr>";
}
echo "</table>";
?>
and in database MySQL:
id | day | hours
1 | mon | 07:00
2 | thu | 08:40
3 | wed | 12:30
4 | thu | 13:00
5 | fri | 05:00
etc
how is the best method for checking this? If date is in database then i would like make TD with red background, for example add class red:
echo "<td at='$col' class='red'>$col</td>";
Check this in each loop FOR is not good idea - this generated to many query for database.
How can i make it?
I can use also jquery and for example JSON if this is possible.
List them all in a keyed array, then when looping through the calendar simply check for these keys.
For example, you'd get the array array(1 => array('07:00' => true)) (day of the week => appointments).
You can then check isset($array[$datetime->format('N')][$datetime->format('H:i')])