两次回声差异 [英] Echo Difference in Two Times

查看:26
本文介绍了两次回声差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PHP 脚本,它以 HH:MM:SS 格式在数据库中收集时间戳.然后我有一个 PHP 页面,它通过 AJAX 每秒刷新一次,其目标是生成一个每秒提取 PHP 数据的脚本,以便我可以执行诸如显示倒数计时器之类的操作.我试图以这种格式显示两次差异:HH:MM:SS.IE:

I have a PHP script that collects a timestamp in a database in the HH:MM:SS format. I then have a PHP page that is refreshed every second via AJAX, the goal of which is to produce a script that pulls PHP data every second so I can do things like display a countdown timer. I am trying to display the difference in two times in this format: HH:MM:SS. IE:

$currentstampedtime = date("H:i:s", strtotime("now"));

产生:

19:29:34

(并正确计算我的 ajax 脚本,显示为时钟.

(and counts up with my ajax script correctly, appears as a clock.

$gametimestamp = date("H:i:s", strtotime($gametime)); 

产生:

19:19:12

通过从我的数据库中提取时间戳,存储在 $gametime 中.

By pulling the timestamp from my DB, stored in $gametime.

我想要做的是像倒数计时器一样实时显示这两个之间的差异,假设 ajax 每秒刷新页面,就像它看起来正在做的那样.

What I am trying to do is get the difference between these two echo'ed out in real time like a countdown timer, assuming the ajax refreshes the page every second like it appears to be doing.

$difference = ($currentstampedtime - $gametimestamp);

什么都不做.当我回显 var 时,

Does nothing. When I echo the var,

echo $difference; 

我明白了

1

我尝试过的任何方法都不起作用.请帮忙.提前致谢.

Nothing I have tried is working. Please help. Thanks in advance.

推荐答案

你可以使用 php DateTime 对象来创建您的当前时间和目标时间.然后,使用 diff() 函数DateTime 对象以获取差异并将其格式化为00:00:00".

You could use php DateTime object to create your current time and target time. Then, use the diff() function of the DateTime object to get the difference and format it as "00:00:00".

即:

$now = new DateTime();
$target = DateTime::createFromFormat('H:i:s', "01:30:00");
$difference = $now->diff($target);
echo $difference->format("%H:%I:%S");

输出:

00:57:48

希望有帮助.

这篇关于两次回声差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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