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!

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

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?