Emil Elkjær
Emil Elkjær

Reputation: 685

Calculate hours between two dates

I am trying to make this overview of "work" hours

However I can't get it to calculate the hours between two dates in my sql db, but it wont return the result.

I tried to just display the two SQL entries which worked, but it seems like the calculation wont use the SQL enteries

I am getting this error in the top of my page:

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

What am I doing wrong here??

The thing I need help to is on line 43-52 Here is my code:

<?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>

Upvotes: 0

Views: 1364

Answers (3)

Emil Elkj&#230;r
Emil Elkj&#230;r

Reputation: 685

Problem solved! Many thanks Ryan Griggs

Change SQL query to $sql="SELECT *, (UNIX_TIMESTAMP(end) - UNIX_TIMESTAMP(start)) / 60.0 / 60.0 as hours_difference FROM $tbl_name ORDER BY id ASC";

Replace

<?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>

with

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

Upvotes: 1

Ryan Griggs
Ryan Griggs

Reputation: 2758

Convert both dates to unix timestamps, then return the difference in hours.

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

No need for any PHP calculations after that...

Upvotes: 3

someone
someone

Reputation: 6572

Change

$date1 = new DateTime($stamp2);  

to this

 $date1 = new DateTime($stamp1);

Upvotes: 1

Related Questions