简体   繁体   English

如何使if语句基于时间戳的日期导致php表?

[英]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.

相关问题 如何基于PHP中的json结果在Javascript中创建IF语句 - How to make IF statement in Javascript based on the json result in PHP 如何基于php表中的时间戳在24小时内自动运行php脚本 - How to run automatically php script very 24 hours based on timestamp from table in php 尝试根据不同表中的日期从表中返回日期时,不确定以下 WHERE 语句是否有效 - Unsure if the following WHERE statement is valid when trying to return dates from a table based on dates in a different table 如何基于掷骰结果使骰子掷骰更新表数据 - How to make a dice roll UPDATE table data based on the roll result in php 根据时间或两个日期从Php / Mysql中的表中选择行? - Select rows from table in Php/Mysql based on time or two dates? 根据 php 结果禁用 DatePicker 中的特定日期 - Disable Specific Dates in DatePicker Based on php Result Select语句,该语句从sql中的时间戳计数匹配的日期(而不是时间) - Select statement that counts matching dates (not time) from a timestamp in sql PHP时间戳和日期问题 - PHP timestamp and dates issue 如何使PHP读取SWF结果? - How to make an SWF read result from a PHP? 如何使 sql 查询在数据库表中根据 id 显示 1 个结果 - How to make sql query to display 1 result based on id in the database table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM