【问题标题】:PHP - Get a time interval of an intervalPHP - 获取时间间隔的时间间隔
【发布时间】:2014-12-30 04:50:43
【问题描述】:

我一直在尝试使用 DateTime 类来计算时间间隔。

我使用来自 $_SERVER['REQUEST_TIME'] 的 unix 时间戳和从数据库中检索的时间来获取时间间隔,即时间 DateInterval。这部分有效。

例如,该时间间隔可能是进程开始后的 5 分 10 秒。

我预计完成该过程的时间是 10 分钟,我想通知用户还有 4 分 50 秒的预计时间。

$running_time; // calculated before, type DateInterval. Echoing the formatted value shows correct time
// (only problem is no leading 0's for single-digit minutes/seconds in format: '%i:%s')
$total = new DateTime('10:00'); // ten minutes, 0 seconds expected total running time
$time_left = $total->diff(new DateTime($running_time->format('%i:%s'))); // fatal error

我认为这会起作用,但奇怪的是,格式功能没有按预期工作。分和秒为个位数时不带前导零。

有什么提示吗?

【问题讨论】:

    标签: php datetime dateinterval


    【解决方案1】:

    是的,PHP 文档在这一点上已关闭。

    将你的 I 和 S 大写,你会得到前导零。

    $running_time->format('%I:%S')
    

    【讨论】:

    • 奇怪的是,它们在这里使用的格式与date() 函数中的格式不同。这似乎奏效了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 2022-08-03
    • 2021-08-21
    • 1970-01-01
    • 1970-01-01
    • 2010-12-03
    • 2022-01-17
    相关资源
    最近更新 更多