Question

I have a PHP script which receives a set of Events from a database with begin/end DateTimes, which represent working times.

Begin               | End
2013-08-14 10:00:00 | 2013-08-22 09:30:00
2013-08-08 07:00:00 | 2013-08-08 15:00:00
2013-08-09 07:00:00 | 2013-08-10 07:00:00

Now I want to calculate how much has been worked each single day. For the first row I would want an output like that:

Begin               | End
2013-08-14 10:00:00 | 2013-08-14 23:59:59
2013-08-15 00:00:00 | 2013-08-15 23:59:59
2013-08-16 00:00:00 | 2013-08-16 23:59:59
....
2013-08-22 00:00:00 | 2013-08-22 09:30:00

I've seen some things with DatePeriod and DateInterval, but those didn't take time into account.

Thanks for your help.

Was it helpful?

Solution

DatePeriod and DateInterval DO take time into account, so you can use those classes.

Your data :

$intervals = [
    ['begin' => '2013-08-14 10:00:00', 'end' => '2013-08-22 09:30:00'],
    ['begin' => '2013-08-08 07:00:00', 'end' => '2013-08-08 15:00:00'],
    ['begin' => '2013-08-09 07:00:00', 'end' => '2013-08-10 07:00:00'],
];

Quick function I wrote :

function explodePeriodByDays($begin, $end) {
    $days = [];
    $dayInterval = new DateInterval('P1D');
    $begin = new DateTime($begin);
    $end = new DateTime($end);
    $_end = clone $end; 
    $_end->modify('+1 day');
    foreach ((new DatePeriod($begin, $dayInterval, $_end)) as $i => $period) {
        $_begin = $period;
        if ($i) $_begin->setTime(0, 0, 0);
        if ($_begin > $end) break;
        $_end = clone $_begin;
        $_end->setTime(23, 59, 59);
        if ($end < $_end) $_end = $end;
        $days[] = [
            'begin' => $_begin,
            'end' => $_end,
        ];
    }
    return $days;
}

Example of function use :

foreach ($intervals as $interval) {
    echo "Day intervals from {$interval['begin']} to {$interval['end']} : \n";
    foreach (explodePeriodByDays($interval['begin'], $interval['end']) as $day) {
        echo "\t {$day['begin']->format('Y-m-d H:i:s')} | {$day['end']->format('Y-m-d H:i:s')}\n";
    }
    echo "\n";
}

Output of the example :

Day intervals from 2013-08-14 10:00:00 to 2013-08-22 09:30:00 : 
     2013-08-14 10:00:00 | 2013-08-14 23:59:59
     2013-08-15 00:00:00 | 2013-08-15 23:59:59
     2013-08-16 00:00:00 | 2013-08-16 23:59:59
     2013-08-17 00:00:00 | 2013-08-17 23:59:59
     2013-08-18 00:00:00 | 2013-08-18 23:59:59
     2013-08-19 00:00:00 | 2013-08-19 23:59:59
     2013-08-20 00:00:00 | 2013-08-20 23:59:59
     2013-08-21 00:00:00 | 2013-08-21 23:59:59
     2013-08-22 00:00:00 | 2013-08-22 09:30:00

Day intervals from 2013-08-08 07:00:00 to 2013-08-08 15:00:00 : 
     2013-08-08 07:00:00 | 2013-08-08 15:00:00

Day intervals from 2013-08-09 07:00:00 to 2013-08-10 07:00:00 : 
     2013-08-09 07:00:00 | 2013-08-09 23:59:59
     2013-08-10 00:00:00 | 2013-08-10 07:00:00

OTHER TIPS

If I am understanding this correctly, you want to get the difference between a begin date and an end date.

I would go about converting the time with a strtotime, subtract the timestamps, then output the date.

$begin = strtotime("2013-08-14 10:00:00");
$end   = strtotime("2013-08-14 23:59:59");

$difference = ($end - $begin);

echo gmdate("H:i:s", $difference);

This would give you the time in hours, minutes, then seconds.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top