dongshi2458 2015-09-06 03:44
浏览 92
已采纳

HTML DateTime.Now减去两小时[关闭]

I'm trying to run conditional formatting inside a insert_mysql.php on data sourced from a MySQL table. Data originates from an Arduino Uno + WiFi Shield that will post a value (any value) and a timestamp each time the button is pressed. A display_mysql.php outputs to HTML so a user can see the result on a web page. For the purposes of this question, the only output the user sees on the page is a single timestamp of the last date/time of a button press, nothing else. I want to format the text according to:
-css_1 if the event occurred less that two hours ago.
-css_2 is the event occurred more than two hours ago.

I tried this but I suspect that there are many flaws. It's only a place to start:

if ($result = mysql_query("SELECT MAX(TimeStamp) AS 'Last Time' FROM {$table}")&lt (%DateTime.Now% - 2)) {
echo '<div class="css_1">';
}
else{
echo '<div class="css_2">';
}

I continue to get a variety of errors related to expected or unexpected characters. This particular code returns: "Parse error: syntax error, unexpected '%', expecting ')' in..."

My question is this... Is there a way to do time calculations within PHP based on MySQL output? The desired outcome is to have a date and time printed on a web page in either green if the event falls within the two hour window, meaning it's a new event, or red if it's older than two hours, meaning it's an old event.

  • 写回答

1条回答 默认 最新

  • douan6931 2015-09-06 06:58
    关注

    Try this...

    <?php
    $host = 'localhost'; $db = 'db-name'; $user = 'db-user'; $pw = 'db-pwd';
    $conn = new PDO('mysql:host='.$host.';dbname='.$db.';charset=utf8', $user, $pw);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    
    try { // let's get our data from the database
        $sql = "SELECT * FROM events";
        $query = $conn->prepare($sql);
        $query->execute();
        $row = $query->fetch(PDO::FETCH_ASSOC);
        $totalRows = $query->rowCount();
    } catch (PDOException $e) {
        die("Could not get the data: " . $e->getMessage());
    }
    
    $now = date('Y-m-d H:i:s', time()); // first we will get our current date & time
    echo '<br>Time now: '.$now.'<br><br>';
    $nowstring = strtotime($now); // convert that to a time string 
    
    do { // start looping through our data
        $timestring = strtotime($row['eventdate']); // convert each of our dates to a time string
        $diff = $nowstring - $timestring; // now substract the event date time string from the current date time string
        $time = date('Y-m-d H:i:s', $timestring); // convert our event date time string back into a human readable date
        echo 'Event Date: '.$time.'<br>'; // echo the event date to the page
        if($diff > 7200) { // if the difference in time strings is greater than 7200, which is the number of seconds in 2 hours
            echo '<div style="color:red">This is old</div><br><br>';
        } elseif($diff < 0) { // if the difference in time strings is a negative number
            echo '<div style="color:blue">This event hasn\'t occurred yet</div><br><br>';
        } else { // otherwise the event date falls within the two our window
            echo '<div style="color:#6c6">This is new</div><br><br>';
        }
    
    } while($row = $query->fetch(PDO::FETCH_ASSOC)); // end the loop
    ?>
    

    The database query was written in pdo_mysql. Feel free to change it, however; I recommend sticking with PDO or at a minimum mysqli.

    This is the process...

    1. Get today's date and convert it to a time string like 1441549135
    2. Then we will loop through our dataset converting each event date to a time string
    3. Subtract the event date time string from the current time string
    4. If the difference is more than 7200 (the number of seconds in 2 hours) then the event is old
    5. If the difference is a negative number, then that event date hasn't arrived yet
    6. Finally, if the difference is less than 7200, the event is new.

    Happy coding!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥50 vue-codemirror如何对指定行 指定位置的 字符进行背景颜色或者字体颜色的修改?
    • ¥15 有人会思科模拟器嘛?
    • ¥30 遇到一个的问题,请教各位
    • ¥20 matlab报错,vflux计算潜流通量
    • ¥15 我该如何实现鼠标按下GUI按钮时就执行按钮里面的操作的方法
    • ¥15 关于#硬件工程#的问题:我这边有个锁相环电路没有效果
    • ¥15 20款 27寸imac苹果一体机装win10后,蓝牙耳机和音响放歌曲卡顿断断续续.
    • ¥15 VB.NET 父窗体调取子窗体报错
    • ¥15 python海龟作图如何改代码使其最后画出来的是一个镜像翻转的图形
    • ¥15 我不明白为什么c#微软的官方api浏览器为什么不支持函数说明的检索,有支持检索函数说明的工具吗?