嘿,伙计们,我正试图弄清楚如何从另一个使用php减去一个时间来获得这两个时间之间的剩余时间。例如
time left = time1-time2
或
timleft = 15:35-15:30
等于还剩5分钟
目前我正在像这样加载两次。
time1来自我的数据库(这是我们正在等待的时间,在我的例子中,我们正在等待的时间是下一次更新的时间),time2是当前系统时间。
我试过使用这个代码
$timeleft = $dbtime - $curtime;
$dbtime =从数据库加载的时间。$curtime =当前系统时间。
但是它只返回一个0
使用strtotime
将日期字符串转换为unix时间戳。
$timeleft = strtotime($dbtime) - strtotime($curtime);
您必须将这两个时间转换为时间戳。strtotime() http://php.net/manual/en/function.strtotime.php是一个很好的函数,它尝试将字符串转换为时间戳。
然后做你所知道的数学然后使用date() http://www.php.net/manual/en/function.date.php函数将你的时间格式化成你喜欢的任何东西
使用strtotime
将字符串转换为unix时间戳:
$timedifference = strtotime($dbtime) - strtotime($curtime); // or also
$timedifference = strtotime($dbtime) - time();
您从另一个字符串中否定一个字符串-结果为0,因为(int)(string) = 0
你必须像这样使用
$dbtime = time();
// query
$timeleft = time() - $dbtime;
参见strtotime()
函数,如果您的日期被它解析