php PHP以分钟为单位获取时差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16265942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:48:44  来源:igfitidea点击:

PHP get time difference in minutes

phpdatedate-math

提问by salty

I looked around and nothing I saw quite worked with my database layout. Basically I need to take the current time, and a predetermined event time, and see if the current time is 10 minutes prior to the event time. This is my test file, it gets the current time into a variable, then pulls the eventTime from the database table, sets the event time to a variable, subtracts the current time from the event time, and prints the difference. The problem is that I need the minutes, and it just prints the hours.

我环顾四周,没有看到任何与我的数据库布局相匹配的东西。基本上我需要获取当前时间和预定的事件时间,并查看当前时间是否比事件时间早 10 分钟。这是我的测试文件,它将当前时间放入一个变量中,然后从数据库表中提取 eventTime,将事件时间设置为一个变量,从事件时间中减去当前时间,并打印出差值。问题是我需要分钟,它只打印小时数。

For example 11:00 - 9:15 gives me 1, where I need it to say 105 (amount of minutes), I have really looked all over, and can't find anything that wouldn't involve changing the db structure.

例如 11:00 - 9:15 给了我 1,我需要它说 105(分钟数),我真的已经看遍了,找不到任何不涉及更改数据库结构的内容。

The database stores event time in 24h format in hours and minutes ex 13:01, and the current time is set in the same format in php using date("H:i")

数据库以 24 小时格式存储事件时间,例如 13:01 的小时和分钟,当前时间在 php 中使用 date("H:i") 以相同格式设置

Here's some test code, the include dbconnect.php is just how I'm connecting to my database.

这是一些测试代码,包括 dbconnect.php 就是我连接到我的数据库的方式。

Thanks for any help you can offer!

谢谢你的尽心帮助!

 <?php
include 'dbconnect.php';

$the_time = date('H:i');
echo $the_time." </br></br>";

$sql="SELECT eventTime FROM events";
$result=$conn->query($sql);

while ($row = $result->fetch_assoc()) {
$new_time=$row['eventTime'];

echo $new_time."New Time</br>";
echo $the_time-$new_time."The Difference</br></br>";
}
?>

Thanks to Gamster Katalin and John Conde it's solved. Here's the working code:

感谢 Gamster Katalin 和 John Conde,它已经解决了。这是工作代码:

<?php
include 'dbconnect.php';

date_default_timezone_set('America/New_York');
$the_time = date('H:i');
echo $the_time." </br></br>";

$sql="SELECT eventTime FROM events";
$result=$conn->query($sql);

while ($row = $result->fetch_assoc()) {
$new_time=$row['eventTime'];

echo $new_time."New Time</br>";
$datetime1 = new DateTime($the_time);
$datetime2 = new DateTime($new_time);
$interval = $datetime1->diff($datetime2);
$hours=$interval->format('%h');
$minutes= $interval->format('%i');
echo $hours." The Difference in Hours</br>".$minutes." The Difference in Minutes</br></br>";
}
?>

回答by John Conde

$datetime1 = new DateTime();
$datetime2 = new DateTime($row['eventTime']);
$interval = $datetime1->diff($datetime2);
$elapsed = $interval->format('%i minutes');
echo $elapsed;

回答by Shomz

Don't forget MySQL is quite powerful for such things:

不要忘记 MySQL 在这些方面非常强大:

SELECT CURTIME() AS currentTime, 
       eventTime, 
       TIME_FORMAT(CURTIME() - TIME(eventTime), '%h:%i' ) AS difference 
FROM events;

Or to get only the difference:

或者只获得差异:

SELECT TIME_FORMAT(CURTIME() - TIME(eventTime), '%h:%i' ) AS difference 
FROM events;