How can the PHP script be adjusted to display only upcoming shows based on the current date and time, filtering out past events?
To display only upcoming shows based on the current date and time, we need to filter out past events by comparing the event date with the current date. We can achieve this by using the PHP `strtotime()` function to convert the event date to a timestamp and then comparing it with the current timestamp obtained using `time()`. By filtering out events where the event date is greater than the current date, we can display only upcoming shows.
<?php
$current_date = time(); // Get the current timestamp
$shows = array(
array('name' => 'Show 1', 'date' => '2022-12-01 18:00:00'),
array('name' => 'Show 2', 'date' => '2022-12-05 20:00:00'),
array('name' => 'Show 3', 'date' => '2022-11-25 15:30:00'),
);
foreach ($shows as $show) {
$event_date = strtotime($show['date']); // Convert event date to timestamp
if ($event_date > $current_date) {
echo $show['name'] . ' - ' . $show['date'] . '<br>'; // Display upcoming shows
}
}
?>
Keywords
Related Questions
- How can PHP developers ensure that session variables are securely utilized to prevent security vulnerabilities in their code?
- What steps can be taken to address the missing data entries in the tables generated by the code?
- What are some common pitfalls to avoid when working with MySQL queries in PHP to prevent errors like the one experienced by the forum user?