What are some best practices for automatically deleting old dates from a timetable using PHP?
When working with timetables in PHP, it is important to automatically delete old dates to keep the schedule up to date and relevant. One way to achieve this is by comparing the current date with the dates in the timetable and removing any dates that have already passed.
// Sample timetable array
$timetable = [
'2022-01-15',
'2022-02-20',
'2022-03-25',
'2022-04-30',
];
// Get current date
$currentDate = date('Y-m-d');
// Loop through timetable and remove old dates
foreach ($timetable as $key => $date) {
if ($date < $currentDate) {
unset($timetable[$key]);
}
}
// Output updated timetable
print_r($timetable);