-1

I'm developing a task management app for my workplace. I would like to be able to calculate how long it took from the task was made, until it was finished.

Pr now my app will log which date and what time (for instance: 2013-09-28 and 14:42) the task was created. It will then also log which date and time the task was marked as completed.

I know I can use MySQL DATEDIFF() to calculate the amount of days it took, but I'm wondering how I can calculate the rest (hours and minutes).

Basicly I want an output that says: "This task took 2 days, 4 hours and 30 minutes to complete."

Anyone have a suggestion for how I can do this?

Thanks in advance for your help.

Best regards, Simen

SAH
  • 49
  • 1
  • 2
  • 8
  • 1
    Look [**here**](http://stackoverflow.com/questions/17182001/get-relative-date-from-the-now-function/17182060#17182060) for examples. – BlitZ Oct 07 '13 at 12:58
  • 1
    http://stackoverflow.com/questions/15688775/php-find-difference-between-two-datetimes/15688824#15688824 – John Conde Oct 07 '13 at 13:00
  • Thanks for your help :) Had already browsed through a lot of different answers here on Stackoverflow, but couldn't find something that made sense for what I was making. But the links you provided HAL9000 was good! :) Cheers. – SAH Oct 07 '13 at 13:07
  • possible duplicate of [Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...](http://stackoverflow.com/a/18602474/67332) – Glavić Oct 07 '13 at 14:48

2 Answers2

1

Using DateTime: (you will want to format result better)

    $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;
Volvox
  • 1,639
  • 1
  • 12
  • 10
1

Use example :

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

Output :

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

Link to the function.

Community
  • 1
  • 1
Glavić
  • 42,781
  • 13
  • 77
  • 107