[英]How to make if statement based on dates from timestamp result in php table?
I'm building a logbook where there will be several entries each day. 我正在建立一个日志,每天会有几个条目。 When I view the logbook I would like to have a small empty space between each date.
当我查看日志时,我希望每个日期之间有一个小空格。
Is there an easy way to compare the dates in my first column to then make an if statement when the date changes? 有没有一种简单的方法来比较我的第一列中的日期,然后在日期更改时创建一个if语句? In pseudocode: If date in column 1 changes: add empty space.
在伪代码中:如果第1列中的日期发生更改:添加空白空间。
This is an extract from the code for my table: 这是我表格代码的摘录:
echo "<tr><td>";
echo date('d.m.y', strtotime($row['timestamp']));
echo "</td><td>";
echo date('G:i', strtotime($row['timestamp']));
echo "</td><td>";
echo $row['value'];
echo "</td></tr>";
Current output: 当前输出:
Date: Time: Value:
01.01.2017 06:00 40
01.01.2017 08:00 35
01.01.2017 10:00 32
02.01.2017 06:00 57
02.01.2017 08:00 42
...
Wanted output: 通缉输出:
Date: Time: Value:
01.01.2017 06:00 40
01.01.2017 08:00 35
01.01.2017 10:00 32
02.01.2017 06:00 57
02.01.2017 08:00 42
...
Store the $row['timestamp'] in a variable at the end of your foreach loop as eg $previousday. 将$ row ['timestamp']存储在foreach循环结尾的变量中,例如$ previousday。 Then you can compare the $row['timestamp'] of the next loop with the $previousday.
然后你可以比较下一个循环的$ row ['timestamp']和$ previousday。 If they're the not the same, you output an extra otherwise, skip the extra
如果它们不相同,则输出额外的,否则跳过额外的
There will be some kind of loop around your code. 代码周围会有某种循环。 In this code you can create a variable $oldDate (or name it however you want it) - and then add the space if it differes from the new data.
在这段代码中,您可以创建一个变量$ oldDate(或者根据需要命名它) - 然后添加空格(如果它与新数据不同)。
// outside of loop
$oldDate = '';
// loop starts here - fills $row somehow
$curDate = date('d.m.y', strtotime($row['timestamp']));
if (!empty($oldDate)) {
if ($oldDate != $curDate) {
echo 'Some space here';
}
}
$oldDate = $curDate;
// now add your code
I assume that you are printing your rows in a loop, so you can add variable which will be holding previous date and then check if it's different. 我假设您在循环中打印行,因此您可以添加将保留上一个日期的变量,然后检查它是否不同。 I think that you can even skip comparing dates and compare only string value.
我认为您甚至可以跳过比较日期并仅比较字符串值。
$previousDate = '';
foreach ($rows as $row) {
// your block of echos
$currentDate = strtotime($row['timestamp']);
if (!empty($previousDate) && ($previousDate != $currentDate)) {
echo '<tr><td colspan="3"></td></tr>';
}
$previousDate = $currentDate;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.