首页 > 解决方案 > 如何将丢失的时间从另一个用 PHP 格式化(H:i:S)的时间变为微秒?

问题描述

我是stackOverflow的新手,我想知道如何从另一个微时间(真)开始将丢失的时间日期设置为微时间(真),并将其格式化为这样(H:i:s)。这是我的代码:

$rewardCountdown = microtime(true) - $this->dailyRewardTime; // daily reward is another microtime(true)
$rewardAvailable = $rewardCountdown >= 60 * 60 * 24; // check if 24h are gone so we can get reward

基本上我想以这种格式(H:i:s)获得$rewardCountdown,我尝试过并且以某种方式得到了类似的东西,但我的时间增加而不是减少

标签: phpdatetimemicrotime

解决方案


当前microtime减去前一个microtime将是两个时间之间经过的秒数的正值。

如果要将秒转换为小时、分钟和秒,只需使用gmdate.

$rewardCountdown = microtime(true) - $this->dailyRewardTime;
$date = gmdate('H:i:s', $rewardCountdown);

推荐阅读