ld1978 发表于 2012-12-18 11:23

奇怪的问题

练习的程序如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>计算离元旦还有多长时间</title>
</head>

<body>
<?php
function run_time(){
        list($msec,$sec) = explode(" ",microtime());
        return ((float)$msec + (float)$sec);
}
        $start_time = run_time();
        $time1 = strtotime(date("Y-m-d H:i:s"));
        $time2 = strtotime("2012-2-8 16:30:00");
        $time3 = strtotime("2013-1-1");
        $sub1 = ceil(($time2 - $time1) / 3600);
        $sub2 = ceil(($time3 - $time1) /86400);
        echo "离放假还有<font color=red> $sub1 </font>小时!!!";
        echo "<p>";
        echo "离2013年元旦还有<font color=red>$sub2</font>天!!!";
        echo "<p>";
        $end_time = run_time();
        //$runtime=$end_time-$start_time;
        //echo $runtime;
?>       
<font color=blue><?php echo"程序运行时间为:".($end_time-$start_time);?></font>秒
</body>
</html>

为什么在网页中输出的是这样的情况呢?

离放假还有 -7530 小时!!!
离2013年元旦还有14天!!!

程序运行时间为:6.5088272094727E-5秒
如标红的部分,而不是计算的结果呢?

ld1978 发表于 2012-12-18 11:25

哦,我理解错了。。。不好意思。这个是结果。

maochanglu 发表于 2012-12-19 10:53

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>计算离元旦还有多长时间</title>
</head>

<body>
<?php
function run_time(){
      list($msec,$sec) = explode(" ",microtime());
      return ((float)$msec + (float)$sec);
}
      $start_time = run_time();
      $time1 = strtotime(date("Y-m-d H:i:s"));
      $time2 = strtotime("2012-2-8 16:30:00");
      $time3 = strtotime("2013-1-1");
      $sub1 = ceil(($time2 - $time1) / 3600);
      $sub2 = ceil(($time3 - $time1) /86400);
      echo "离放假还有<font color=red> $sub1 </font>小时!!!";
      echo "<p>";
      echo "离2013年元旦还有<font color=red>$sub2</font>天!!!";
      echo "<p>";
      $end_time = run_time();
      //$runtime=$end_time-$start_time;
      //echo $runtime;
?>      
<font color=blue><?php echo"程序运行时间为:".($end_time-$start_time);?></font>秒
</body>
</html>
页: [1]
查看完整版本: 奇怪的问题