<?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" => 3856, "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" => 7897, "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 associative arrays to store counts and missing days

$survedCarCounts = array();

$carUsageCount = array();

$missingDays = array();


// Iterate through bookings to count occurrences and calculate missing days

foreach ($bookings as $booking) {

    $carNo = $booking['car_no'];

    $survedCar = $booking['surved_car'];

    

    // Increment usage count for surved car

    $carUsageCount[$survedCar] = ($carUsageCount[$survedCar] ?? 0) + 1;

    

    // Increment occurrence count for replaced type

    if ($booking['type'] === 'replaced') {

        $survedCarCounts[$survedCar] = ($survedCarCounts[$survedCar] ?? 0) + 1;

    }


    // Calculate missing days for the same car number

    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'];

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

     $survedCar = $booking['surved_car'] . '(' . ($carUsageCount[$booking['surved_car']] ?? 0) . ')';

    echo "<tr>";

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

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

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

    echo "<td>" . ($carUsageCount[$booking['surved_car']] ?? 0) . "</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