How can timestamps be effectively used to sort and display upcoming events in a PHP calendar?

When displaying upcoming events in a PHP calendar, timestamps can be effectively used to sort the events based on their start time. By converting the event start times to timestamps, you can easily compare and order them in ascending order. This allows you to display the events in chronological order on the calendar.

// Sample array of events with timestamps
$events = [
    ['name' => 'Event 1', 'start_time' => strtotime('2022-09-15 10:00')],
    ['name' => 'Event 2', 'start_time' => strtotime('2022-09-20 15:00')],
    ['name' => 'Event 3', 'start_time' => strtotime('2022-09-25 09:00')],
];

// Sort events based on start time
usort($events, function($a, $b) {
    return $a['start_time'] - $b['start_time'];
});

// Display sorted events
foreach ($events as $event) {
    echo $event['name'] . ' - ' . date('Y-m-d H:i', $event['start_time']) . '<br>';
}