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.

duanlangwen9597
duanlangwen9597 你的桌子结构是什么?
大约 5 年之前 回复
dongtu7205
dongtu7205 我的第一个建议是写入mysqli或更好的pdo_mysql。mysql在很久以前就被弃用了,很快就会完全被删除。然后你的mysql代码将停止运行。
大约 5 年之前 回复
douyue8685
douyue8685 我不知道你要做什么,但是在mysql_query函数之后,甚至不是PHP那个奇怪的垃圾。根据你的例子,在PHP中应该是什么意思?或%DateTime.Now%?
大约 5 年之前 回复

1个回答

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!

douchun5969
douchun5969 我很欣赏建设性的批评。 我会记住你的意见以备将来的答案。 OP可以从PDO更改为选择。 底层代码仍将按预期运行。:)
大约 5 年之前 回复
duanchai0028
duanchai0028 更好的答案。 使用PDO的荣誉虽然可能使得答案对OP的帮助不那么有用。 另外,使用DateTime()代替日期数学而不是strtotime()。
大约 5 年之前 回复
douzhenggui8171
douzhenggui8171 ...看到我更新的答案
大约 5 年之前 回复
duan1396
duan1396 他们为什么要试试呢? 你不告诉他们任何事情。 你刚刚删除了一堆代码。
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐