To achieve the functionality of changing a badge based on the date, you can use a combination of PHP and JavaScript (or just PHP if you're working server-side only) to determine the status of each dispatch. Here's a simple approach using PHP:
- Fetch the dispatch date from the database.
- Calculate the difference between the current date and the dispatch date.
- Determine the badge status based on the difference.
Here's a basic example using PHP:
<?php
// Example dispatch date from the database
$dispatchDate = '2024-12-12';
// Convert the dispatch date to a DateTime object
$dispatchDateTime = new DateTime($dispatchDate);
// Get the current date
$currentDateTime = new DateTime();
// Calculate the difference in days
$interval = $currentDateTime->diff($dispatchDateTime);
$daysDifference = (int)$interval->format('%r%a');
// Determine the badge status
if ($daysDifference > 14) {
$badgeStatus = 'Dispatched'; // Success
$badgeClass = 'success';
} elseif ($daysDifference >= 0 && $daysDifference <= 14) {
$badgeStatus = 'Due Soon'; // Warning
$badgeClass = 'warning';
} else {
$badgeStatus = 'Over Due'; // Danger
$badgeClass = 'danger';
}
// Output the badge
echo "<span class='badge badge-$badgeClass'>$badgeStatus</span>";
?>
Explanation:
-
DateTime Objects: We use
DateTimeobjects to handle date comparisons easily. -
Date Difference: The
diffmethod calculates the difference between the current date and the dispatch date. -
Badge Logic:
- If the dispatch date is more than 14 days away, it's considered "Dispatched".
- If the dispatch date is within 14 days, it's "Due Soon".
- If the dispatch date has passed, it's "Over Due".
Considerations:
-
Time Zones: Ensure that your server's time zone is set correctly, or adjust the
DateTimeobjects to use the correct time zone. -
Styling: The badge classes (
success,warning,danger) are assumed to be styled with CSS. You can use Bootstrap classes or define your own.
This solution should help you dynamically change the badge status based on the dispatch date. Adjust the logic as needed to fit your specific requirements.