-1

我正在为我的工作场所开发一个任务管理应用程序。我希望能够计算从完成任务到完成任务需要多长时间。

现在我的应用程序将记录创建任务的日期和时间(例如:2013-09-28 和 14:42)。然后它还将记录任务被标记为已完成的日期和时间。

我知道我可以使用 MySQL DATEDIFF() 来计算所需的天数,但我想知道如何计算其余的(小时和分钟)。

基本上我想要一个输出说: “这个任务需要 2 天 4 小时 30 分钟才能完成。”

有人对我如何做到这一点有建议吗?

在此先感谢您的帮助。

最好的问候,西门

4

2 回答 2

1

使用示例:

echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('2013-05-01 00:22:35', true);

输出 :

4 months ago
4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago

链接到函数。

于 2013-10-07T14:51:19.217 回答
1

使用 DateTime:(您将希望更好地格式化结果)

    $q = new DateTime('2013-10-11 10:10:11');
    $w = new DateTime('2013-11-18 14:13:16');
    $result = $w->diff($q);
    $string = "This task took ";
    if ($result->y > 0) {$string .= $result->y.' years, ';}
    if ($result->m > 0) {$string .= $result->m.' months, ';}
    if ($result->d > 0) {$string .= $result->d.' days, ';}
    if ($result->h > 0) {$string .= $result->h.' hours, ';}
    if ($result->i > 0) {$string .= $result->i.' minutes, ';}
    if ($result->s > 0) {$string .= $result->s.' seconds ';}
    $string .= "to complete.";
    echo $string;
于 2013-10-07T13:10:01.547 回答