计算两个日期之间的小时数

时间:2012-10-31 18:59:31

标签: php mysql sql

我正在尝试概述“工作”时间

但是我无法计算我的sql db中两个日期之间的小时数,但它不会返回结果。

我试图只显示两个有用的SQL条目,但似乎计算不会使用SQL enteries

我在页面顶部收到此错误:

Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct() [<a href='datetime.--construct'>datetime.--construct</a>]: Failed to parse time string (31/10-2012 19:14) at position 0 (3): Unexpected character' in D:\xampp\htdocs\admin.php:46 Stack trace: #0 D:\xampp\htdocs\admin.php(46): DateTime->__construct('31/10-2012 19:1...') #1 {main} thrown in D:\xampp\htdocs\admin.php on line 46

我在这里做错了什么?

我需要帮助的是43-52号线 这是我的代码:

<?php
$host="localhost";
$username="xxxx";
$password="xxxx";
$db_name="xxxxx";
$tbl_name="log";


mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$sql="SELECT * FROM $tbl_name ORDER BY id ASC";
$result=mysql_query($sql);


?>
<title>VT Log - Oversigt</title>
<table width="90%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<td width="30%" align="center" bgcolor="#E6E6E6"><strong>Navn</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Start</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Slut</strong></td>
<td width="30%" align="center" bgcolor="#E6E6E6"><strong>Kommentar</strong></td>
<td width="15%" align="center" bgcolor="#E6E6E6"><strong>Tid</strong></td>
</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['user']; ?><BR></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['start']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['end']; ?></td>
<td align="center" bgcolor="#FFFFFF"><? echo $rows['comment']; ?></td>
<?php
$stamp1 = $rows['start'];
$stamp2 = $rows['end'];
$date1 = new DateTime($stamp2);
$date2 = new DateTime($stamp2);
$diff = $date2->diff($date1);
$hours = $diff->h;
$hours = $hours + ($diff->d*24);
?>
<td align="center" bgcolor="#FFFFFF"><? echo $hours;?></td>
</tr>

<?php
}
mysql_close();
?>
<tr>
<td colspan="5" align="center" bgcolor="#E6E6E6"><?php echo "Total arbejdstid: " . array_sum($numbers) . "\n Timer";?></td>
</tr>
</table>

3 个答案:

答案 0 :(得分:3)

将两个日期转换为unix时间戳,然后以小时为单位返回差异。

SELECT (UNIX_TIMESTAMP(end) - UNIX_TIMESTAMP(start)) / 60.0 / 60.0 as hours_difference

之后不需要任何PHP计算......

答案 1 :(得分:1)

更改

$date1 = new DateTime($stamp2);  

到这个

 $date1 = new DateTime($stamp1);

答案 2 :(得分:1)

问题解决了!非常感谢Ryan Griggs

将SQL查询更改为$sql="SELECT *, (UNIX_TIMESTAMP(end) - UNIX_TIMESTAMP(start)) / 60.0 / 60.0 as hours_difference FROM $tbl_name ORDER BY id ASC";

替换

<?php
$stamp1 = $rows['start'];
$stamp2 = $rows['end'];
$date1 = new DateTime($stamp2);
$date2 = new DateTime($stamp2);
$diff = $date2->diff($date1);
$hours = $diff->h;
$hours = $hours + ($diff->d*24);
?>
<td align="center" bgcolor="#FFFFFF"><? echo $hours;?></td>

<td align="center" bgcolor="#FFFFFF"><?$var = number_format($rows['hours_difference'],2);
$var = number_format($var,1);
echo $var; ?></td>