<?php

// Sample data

$bookings = array(

    array("car_no" => 1234, "surved_car" => 1234, "allocation_from_date" => "20-02-2024 10:00", "allocation_to_date" => "20-03-2024 12:00", "lease_start_date" => "20-02-2024", "lease_end_date" => "20-02-2025", "type" => "allocated"),

    array("car_no" => 1234, "surved_car" => 3856, "allocation_from_date" => "28-03-2024 10:00", "allocation_to_date" => "27-04-2024 12:00", "lease_start_date" => "28-03-2024", "lease_end_date" => "28-03-2025", "type" => "replaced"),

    array("car_no" => 6989, "surved_car" => 6989, "allocation_from_date" => "21-01-2024 10:00", "allocation_to_date" => "21-02-2024 12:00", "lease_start_date" => "21-01-2024", "lease_end_date" => "21-01-2025", "type" => "allocated"),

    array("car_no" => 5898, "surved_car" => 3856, "allocation_from_date" => "28-04-2024 10:00", "allocation_to_date" => "28-05-2024 12:00", "lease_start_date" => "28-04-2024", "lease_end_date" => "28-04-2025", "type" => "allocated")

);


// Initialize an associative array to store car usage count

$carUsageCount = array();

// Initialize an associative array to store missing days

$missingDays = array();

// Initialize an associative array to store the occurrence count for each surved car

$survedCarCounts = array();


// Iterate through each booking

foreach ($bookings as $booking) {

    $carNo = $booking['car_no'];

    $survedCar = $booking['surved_car'];

    // Increment car usage count

    if (isset($carUsageCount[$survedCar])) {

        $carUsageCount[$survedCar]++;

    } else {

        $carUsageCount[$survedCar] = 1;

    }

    

    // Increment occurrence count for the specific surved car

    if (isset($survedCarCounts[$survedCar])) {

        $survedCarCounts[$survedCar]++;

    } else {

        $survedCarCounts[$survedCar] = 1;

    }


    // Calculate missing days for the same car

    if (isset($missingDays[$carNo])) {

        $allocationFromDate = strtotime($booking['allocation_from_date']);

        $prevAllocationToDate = strtotime($missingDays[$carNo]['allocation_to_date']);

        $missingDays[$carNo]['missing_days'] += floor(($allocationFromDate - $prevAllocationToDate) / (60 * 60 * 24));

        $missingDays[$carNo]['allocation_to_date'] = $booking['allocation_to_date'];

    } else {

        $missingDays[$carNo] = array(

            'allocation_to_date' => $booking['allocation_to_date'],

            'missing_days' => 0

        );

    }

}


// Output the results in table format

echo "<table border='1'>";

echo "<tr><th>Car No</th><th>Surved Car</th><th>Missing Days</th><th>Usage Count</th></tr>";

foreach ($bookings as $booking) {

    $carNo = $booking['car_no'];

    // Include the occurrence count in the surved car

    $survedCar = $booking['surved_car'] . '(' . $survedCarCounts[$booking['surved_car']] . ')';

    echo "<tr>";

    echo "<td>" . $carNo . "</td>";

    echo "<td>" . $survedCar . "</td>";

    echo "<td>" . ($missingDays[$carNo]['missing_days'] ?? 0) . "</td>";

    echo "<td>" . $carUsageCount[$booking['surved_car']] . "</td>";

    echo "</tr>";

}

echo "</table>";

?>


Comments

Popular posts from this blog

How to seperate character from string in php

How to Delete record using PHP Ajax

Uploads Only 10 files in month step by step