简体   繁体   English

php 时间搞砸了

[英]php time messed up

i need help creating a count down timer in php.我需要帮助在 php 中创建倒数计时器。 the problem is that i want to store an INT in my database which is going to be seconds, and this int is going to be added to current time to represent the future time.问题是我想在我的数据库中存储一个 INT,这将是几秒钟,并且这个 int 将被添加到当前时间以表示未来时间。

now if i try to subtract the current time from future time to show how many seconds remaining, am getting wrong date.现在,如果我尝试从将来的时间中减去当前时间以显示剩余的秒数,那么日期会出错。

here is my schema这是我的架构

mysql> desc buildings;
+-----------+-------------+------+-----+---------+----------------+
| Field     | Type        | Null | Key | Default | Extra          |
+-----------+-------------+------+-----+---------+----------------+
| id        | int(11)     | NO   | PRI | NULL    | auto_increment |
| name      | varchar(20) | YES  |     | NULL    |                |
| level     | int(2)      | YES  |     | NULL    |                |
| created   | int(11)     | NO   |     | NULL    |                |
| finished  | int(11)     | YES  |     | NULL    |                |
| player_id | int(11)     | YES  | MUL | NULL    |                |
| position  | int(2)      | YES  |     | NULL    |                |
+-----------+-------------+------+-----+---------+----------------+

and heres the code to check这是要检查的代码


//starting session;
//connecting to database;

$query = "select created, finished from buildings where id = 1";
$query = mysql_query($query) or die ("could not query 
".mysql_error() ); while($row = mysql_fetch_assoc($query)) { $created = $row['created']; $finished = $row['finished']; } $CurrentTime = time(); $status = $finished - $CurrentTime; $realstatus = date("H:i:s",$status); if($status > 0) { echo "under construction, still ".$realstatus." to finsih"; }else { die("construction complete"); } //echo $created."
".$finished; ?>

any help will be greatly appreciated.任何帮助将不胜感激。

Can't offer much of an answer without seeing your code, but maybe this will provide some insight:如果没有看到您的代码,无法提供太多答案,但也许这会提供一些见解:

<?php
    $fmt = 'Y-m-d H:i.s';                            // date formatting
    $now = time();                                   // current time
    $offset = 600;                                   // future offset
    $future = strtotime('+' . $offset . ' seconds'); // future time using relative time
    $timeleft = $future - $now;                      // time left in seconds

    // echo results
    echo 'Current Time: ' . date($fmt, $now) . PHP_EOL;
    echo 'Event Time: ' . date($fmt, $future) . PHP_EOL;
    echo 'Time Until Event: ' . $timeleft . ' seconds' . PHP_EOL;
?>

Output: Output:

Current Time: 2011-05-03 12:00.15
Event Time: 2011-05-03 12:10.15
Time Until Event: 600 seconds

Your problem is that you are subtracting two timestamps and format the result as a date.您的问题是您要减去两个时间戳并将结果格式化为日期。 The result is not a date, it´s an interval between two dates.结果不是日期,而是两个日期之间的间隔。

If you want to know how many hours / minutes / seconds are remaining, you just have to divide your result $status through 3600 / 60 / 1 (divide for hours and take the remainder to divide for minutes etc.).如果您想知道还剩多少小时/分钟/秒,您只需将结果$status除以 3600 / 60 / 1 (除以小时,将余数除以分钟等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM