如何显示MySQL中两个时间字段之间的分钟数?

时间:2022-11-03 09:07:13

I'm working on a time management system and am currently stuck. I'm trying to use PHP to calculate the number of hours/minutes between two columns (from and to). These are both set as 'time' type so MySQL recognises it as a time datatype.

我正在研究一个时间管理系统,目前我被困住了。我正在尝试使用PHP来计算两列之间的小时/分钟数(从和到)。这些都设置为'time'类型,因此MySQL将其识别为时间数据类型。

I've tried the following in PHP:

我在PHP中尝试了以下内容:

$from= isset($_GET['from']) ? '%'.$_GET['from'].'%' : '';    
$to = isset($_GET['to']) ? '%'.$_GET['to'].'%' : '';    
$diff = $to - $from;    


echo "<table border='1'>    
echo "<tr>";    
echo "<td>" . $row[$diff] . "</td>";    
echo "</tr>";    
echo "</table>"; 

Say for example 'to' is set at 15:00:00 and from is set at 09:00:00, then the time difference for that row should be 6 hours and this should be displayed for that row.

比如说'to'设置为15:00:00,from设置为09:00:00,那么该行的时差应该是6小时,应该显示该行。

The variable $difference is supposed to echo the difference in hours/minutes but it just displays the id assigned to that row instead. I'm trying to echo the number of minutes for each row in the table not just one.

变量$ difference应该以小时/分钟的形式回显差异,但它只显示分配给该行的id。我试图回应表格中每行的分钟数而不仅仅是一行。

I've seen the timediff function but don't know if that will do the trick.

我已经看过timediff函数,但不知道是否可以解决这个问题。

1 个解决方案

#1


0  

I don't see your logic with the % and the $_GET vars, and $row suddenly appears out of nowhere. You should convert the inputted times to php DateTime classes. An example shows calculating diffs: http://nl1.php.net/manual/en/class.datetime.php#108970 Create your DateTime for example like this: $dateFrom = new DateTime("2014-02-24 ".$_GET['from']); $dateTo = new DateTime("2014-02-24 ".$_GET['to']); $diff = $dateFrom->diff($dateTo); print_r($diff) ; The day you use is not really important if you only need the difference in time.

我没有看到你的%和$ _GET变量的逻辑,$ row突然出现了。您应该将输入的时间转换为php DateTime类。示例显示计算差异:http://nl1.php.net/manual/en/class.datetime.php#108970创建您的DateTime,例如:$ dateFrom = new DateTime(“2014-02-24”。$ _从获得']); $ dateTo = new DateTime(“2014-02-24”。$ _ GET ['to']); $ diff = $ dateFrom-> diff($ dateTo); print_r($ diff);如果您只需要时间上的差异,那么您使用的日子并不重要。

#1


0  

I don't see your logic with the % and the $_GET vars, and $row suddenly appears out of nowhere. You should convert the inputted times to php DateTime classes. An example shows calculating diffs: http://nl1.php.net/manual/en/class.datetime.php#108970 Create your DateTime for example like this: $dateFrom = new DateTime("2014-02-24 ".$_GET['from']); $dateTo = new DateTime("2014-02-24 ".$_GET['to']); $diff = $dateFrom->diff($dateTo); print_r($diff) ; The day you use is not really important if you only need the difference in time.

我没有看到你的%和$ _GET变量的逻辑,$ row突然出现了。您应该将输入的时间转换为php DateTime类。示例显示计算差异:http://nl1.php.net/manual/en/class.datetime.php#108970创建您的DateTime,例如:$ dateFrom = new DateTime(“2014-02-24”。$ _从获得']); $ dateTo = new DateTime(“2014-02-24”。$ _ GET ['to']); $ diff = $ dateFrom-> diff($ dateTo); print_r($ diff);如果您只需要时间上的差异,那么您使用的日子并不重要。