如何使用PHP找出MySQL中缺少的日期?
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>";
目前的结果
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
我想要这样的结果
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
答案 0 :(得分:0)
所有遗失的签到都未分配给任何用户。如果要获取分配给用户ali或未分配的记录,则在两者之间给出日期:
SELECT * FROM attend WHERE (user_name='ali' OR user_name = '') AND date BETWEEN '2015-07-01' AND '2015-07-15' order by date
如果您向我们展示您的架构,那将会更加清晰。
答案 1 :(得分:0)
您可以使用函数中的范围执行此操作,这将允许您重用代码:
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');
...
}
这个功能允许在一个时间间隔内显示每一天的参与者。
然后:
getAttendInInterval('2015-07-01','2015-07-15');
答案 2 :(得分:0)
我会在该日期列中使用时间戳。如果你这样做,你可以简单地“检查”预计接下来会是哪一天。如果它不是那一天,那么按照你想要的那天打印然后继续,如果你有预期的日期。
这样做,您应该在当天的给定时间保存所有时间戳。总是像12:00:00。这使得处理这些数字变得更加容易。
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>";
另一种方法是在使用我发布的代码之前,首先将日期转换为时间戳。你也可以在这里使用“strtotime”-Func。只是添加时间,只是为了确定。 :)
答案 3 :(得分:-2)
您可以在LOOP中使用日期,如果日期在数据库中匹配,则显示所有信息。带有日期的数据库,否则只显示日期。