用php数据库倒计时

时间:2014-05-19 13:28:10

标签: php mysql sql database countdown

我正在尝试使用php和数据库创建倒计时。我知道我可以在没有数据库的情况下完成,但这是针对学校项目的,我们需要使用数据库。

这是我到目前为止所得到的。但$ days_remaining的值为-16210。

<?php 
include("php/db_connect.php");

//TODAY'S DATE
$today = time();

//FETCHES DATE AND TIME FOR THE EVENT FROM DATABASE
$sql = "SELECT * FROM countdown";

$result = mysql_query($sql) or die(mysql_error());

$date = mysql_fetch_array( $result );


//CALCULATES SECONDS UNTIL THE EVENT
$remaining = $date['date'] - $today;

//CALCULATE DAYS UNTIL THE EVENT
$days_remaining = floor($remaining / 86400);

echo "<div class='countdown'> $days_remaining days until event</div>";

?>

1 个答案:

答案 0 :(得分:0)

您没有将日期转换为Unix时间戳。结果你的数学将无法运作。变化:

//CALCULATES SECONDS UNTIL THE EVENT
$remaining = $date['date'] - $today;

为:

//CALCULATES SECONDS UNTIL THE EVENT
$remaining = strtotime($date['date']) - $today;