PHP在数分钟内获得时差 [英] PHP get time difference in minutes

查看:81
本文介绍了PHP在数分钟内获得时差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

例如11:00-9:15给我1,我需要在上面说105(分钟数),我的视线已经遍地开花,找不到任何与更改db结构无关的东西。

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.

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

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")

测试代码,include dbconnect.php就是我连接数据库的方式。

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

感谢您提供的任何帮助!

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>";
}
?>

感谢Gamster Katalin和John Conde的解决。这是工作代码:

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

<?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>";
}
?>


推荐答案

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

这篇关于PHP在数分钟内获得时差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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