php 每天循环几次
php loop with several times per day
我有一个 mysql table 看起来像这样
id date day
1 2016-03-10 15:00:00 monday
2 2016-03-10 16:00:00 monday
3 2016-03-10 17:00:00 monday
4 2016-03-11 15:00:00 tuesday
5 2016-03-11 16:00:00 tuesday
6 2016-03-11 17:00:00 tuesday
7 2016-03-11 18:00:00 tuesday
然后我使用以下 php 代码提取从现在到 3 个月之间的日期和时间信息
<?php
session_start();
if(isset($_SESSION["userID"])){
$start=date("Y-m-d H:i:s");
echo $stop= date('Y-m-d H:i:s', strtotime("+3 months", strtotime($start)));
$result2 = $con->query("select * from event WHERE start_at BETWEEN '".$start."' AND '".$stop."'
ORDER by start_at ASC");
}else{
header('Location: Login.php');
}
?>
然后在我的 html 正文中输入了类似这样的内容
<?php
echo "<table><thead><tr><th>Day</th><th>Time</th></tr></thead>";
// output data of each row
while($row = $result2->fetch_assoc()) {
echo "<tr><td>".$row["name"]."</td><td> ".date('G:i', strtotime($row["start_at"]))."</td></tr>";
}
echo "</table>";
?>
生成以下结果
Day Time
Monday 15:00
Monday 16:00
等等。但是我想要的结果是这样的
Monday 2016-03-10
15:00
16:00
17:00
Tuesday 2016-03-11
15:00
16:00
17:00
18:00
关于如何更改我的代码以获得所需输出的想法?
我刚刚根据您的 mySQL table 结构 编辑了最后一个代码片段 。 未测试。但我希望你能明白:
<?php
$day = null;
echo "<table><thead><tr><th>--</th><th>--</th></tr></thead>";
while($row = $result2->fetch_assoc()) {
if($day != $row["name"]) {
$day = $row["name"];
echo "<tr><td>".$row["name"]."</td><td> ".strstr($row["date"], " ", -1)."</td></tr>";
} else {
echo "<tr><td>".strstr($row["start_at"], " ")."</td><td></td></tr>";
}
}
echo "</table>";
?>
编辑:
我只是注意到数据库中的时间来自名为 "start_at" 的行。修复了代码。
这就是你的需求。
<?php
$start=date("Y-m-d H:i:s");
$stop= date('Y-m-d H:i:s', strtotime("+3 months", strtotime($start)));
$result2 = $con->query("select * from event WHERE date BETWEEN '".$start."' AND '".$stop."' ORDER by date ASC");
echo "<table>";
$day = null;
while($row = $result2->fetch_assoc()) {
if($day != $row['day']){
$day = $row["day"];
echo "<tr><td>".ucfirst($row['day'])."</td><td>".date('Y-m-d', strtotime($row["date"]))."</td></tr>";
echo "<tr><td colspan='2'>".date('G:i', strtotime($row["date"]))."</td></tr>";
}else{
echo "<tr><td colspan='2'>".date('G:i', strtotime($row["date"]))."</td></tr>";
}
}
echo "</table>";
?>
我有一个 mysql table 看起来像这样
id date day
1 2016-03-10 15:00:00 monday
2 2016-03-10 16:00:00 monday
3 2016-03-10 17:00:00 monday
4 2016-03-11 15:00:00 tuesday
5 2016-03-11 16:00:00 tuesday
6 2016-03-11 17:00:00 tuesday
7 2016-03-11 18:00:00 tuesday
然后我使用以下 php 代码提取从现在到 3 个月之间的日期和时间信息
<?php
session_start();
if(isset($_SESSION["userID"])){
$start=date("Y-m-d H:i:s");
echo $stop= date('Y-m-d H:i:s', strtotime("+3 months", strtotime($start)));
$result2 = $con->query("select * from event WHERE start_at BETWEEN '".$start."' AND '".$stop."'
ORDER by start_at ASC");
}else{
header('Location: Login.php');
}
?>
然后在我的 html 正文中输入了类似这样的内容
<?php
echo "<table><thead><tr><th>Day</th><th>Time</th></tr></thead>";
// output data of each row
while($row = $result2->fetch_assoc()) {
echo "<tr><td>".$row["name"]."</td><td> ".date('G:i', strtotime($row["start_at"]))."</td></tr>";
}
echo "</table>";
?>
生成以下结果
Day Time
Monday 15:00
Monday 16:00
等等。但是我想要的结果是这样的
Monday 2016-03-10
15:00
16:00
17:00
Tuesday 2016-03-11
15:00
16:00
17:00
18:00
关于如何更改我的代码以获得所需输出的想法?
我刚刚根据您的 mySQL table 结构 编辑了最后一个代码片段 。 未测试。但我希望你能明白:
<?php
$day = null;
echo "<table><thead><tr><th>--</th><th>--</th></tr></thead>";
while($row = $result2->fetch_assoc()) {
if($day != $row["name"]) {
$day = $row["name"];
echo "<tr><td>".$row["name"]."</td><td> ".strstr($row["date"], " ", -1)."</td></tr>";
} else {
echo "<tr><td>".strstr($row["start_at"], " ")."</td><td></td></tr>";
}
}
echo "</table>";
?>
编辑:
我只是注意到数据库中的时间来自名为 "start_at" 的行。修复了代码。
这就是你的需求。
<?php
$start=date("Y-m-d H:i:s");
$stop= date('Y-m-d H:i:s', strtotime("+3 months", strtotime($start)));
$result2 = $con->query("select * from event WHERE date BETWEEN '".$start."' AND '".$stop."' ORDER by date ASC");
echo "<table>";
$day = null;
while($row = $result2->fetch_assoc()) {
if($day != $row['day']){
$day = $row["day"];
echo "<tr><td>".ucfirst($row['day'])."</td><td>".date('Y-m-d', strtotime($row["date"]))."</td></tr>";
echo "<tr><td colspan='2'>".date('G:i', strtotime($row["date"]))."</td></tr>";
}else{
echo "<tr><td colspan='2'>".date('G:i', strtotime($row["date"]))."</td></tr>";
}
}
echo "</table>";
?>