计算数据库的总工作时间 [英] Calculating total hours out of database

查看:101
本文介绍了计算数据库的总工作时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP登录,显示了我所做的所有事情.

I have a Log in PHP which shows all the things I've done.

结构类似于:

日期-完成的事情-小时

Date -- Things done -- Hours

我想出了如何在表格中显示所有信息.但是我想在底部有一个总小时数,用于计算数据库的所有小时数.

I figured how to show all the info in a table. But I want to have a total hours in the bottom which calculated all the hours of the database.

我每天都会添加一个新日志(那天我做了什么).但是我想知道我该怎么做.我已经搜索了一段时间,但没有成功..

I'm adding every day a new log (what I've done that day). But I wanna know how I can do this. I've been searching for a while now, without success..

我希望任何人都能帮助我.

I hope anyone can help me.

谢谢.

这是我显示代码(简短版本)的方式:

This is how I display my code(short version):

 <tr>
        <td class="date"><?php echo $row['Date'] ?></td>
        <td><?php echo $row['Detail'] ?></td>
        <td><?php echo $row['Hours'] ?></td>
    </tr>

为澄清起见:

我创建了一个新的td,通过添加所有小时数来回显总时数.

I make a new td which echo's the total hours by adding all the hours.

因此,如果我的小时数在小时表中是8 + 8 + 8 + 8 + 4 + 5,则它将显示为:

So if my hours are 8 + 8 + 8 + 8 + 4 + 5 in the hours table it will be shown as:

总小时:41小时

推荐答案

要显示总小时数,请执行以下操作:

To display the total sum of hours, you do this:

$db = new mysqli( DB_HOST, DB_USER, DB_PASS, DB_DATABASE );
$sql = 'SELECT SUM( `Hours` ) AS `total` FROM `table_name`';
$res = $db->query( $sql ) or die( 'MySQL error ' . $db->error );
$row = $res->fetch_assoc();
echo 'Total: ' . $row['total'];

如果date是典型的datetime/date/timestamp,则可以像这样获取每天的总计(如果date是date类型,则不需要DATE( date )):

If date is of typ datetime/date/timestamp you can get the total for every day like this (If the date is of type date, you do not need DATE(date)):

$sql = 'SELECT DATE(`date`) AS `day`, SUM(`Hours`) as `total` '
     . 'FROM `table_name` GROUP BY DATE(`date`) ORDER BY `date`';
$res = $db->query( $sql ) or die( 'MySQL error ' . $db->error );
while( $row = $res->fetch_assoc() )
    echo $row['day'] . ' has ' . $row['total'] . ' hours' . PHP_EOL;

如果date是典型的int或PHP时间戳,您可以像这样每天获取总计:

If date is of typ int or PHP timestamp you can get the total for every day like this:

$sql = 'SELECT DATE( FROM_UNIXTIME(`date`) ) AS `day`, SUM(`Hours`) as `total` '
     . 'FROM `table_name` GROUP BY DATE(FROM_UNIXTIME(`date`)) ORDER BY `date`';
$res = $db->query( $sql ) or die( 'MySQL error ' . $db->error );
while( $row = $res->fetch_assoc() )
    echo $row['day'] . ' has ' . $row['total'] . ' hours' . PHP_EOL;

这篇关于计算数据库的总工作时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆