Skip to content
PHP Logo

Are there any built-in PHP functions or methods that can streamline the process of calculating weekly work hour summaries in a monthly overview?

To streamline the process of calculating weekly work hour summaries in a monthly overview, you can use the `strtotime()` function to convert dates to timestamps, and then calculate the difference in hours between two timestamps. You can loop through the work hours data for each day in a month and group them by week to calculate the total work hours for each week.

php
// Sample work hours data for a month
$workHours = [
    '2022-01-01' => 8,
    '2022-01-02' => 7,
    // Add more work hours data for the month
];

$weeklyHours = [];
$weekNumber = 1;

foreach ($workHours as $date => $hours) {
    $timestamp = strtotime($date);
    $week = date('W', $timestamp);

    if (!isset($weeklyHours[$week])) {
        $weeklyHours[$week] = 0;
    }

    $weeklyHours[$week] += $hours;
}

// Output weekly work hour summaries
foreach ($weeklyHours as $week => $hours) {
    echo "Week $week: $hours hours worked\n";
}