Menu
Coddy logo textTech

Listing Upcoming Events

Part of the Logic & Flow section of Coddy's PHP journey — lesson 61 of 68.

challenge icon

Challenge

Easy

You will receive two inputs: a JSON string representing the existing events array, and a date string in YYYY-MM-DD format representing today's date.

The first input will be a JSON string in this format: [{"name":"Team Meeting","date":"2024-04-15","location":"Office"},{"name":"Conference","date":"2024-05-20","location":"Convention Center"},{"name":"Workshop","date":"2024-03-10","location":"Library"}]

The second input will be a date string in this format: 2024-04-01

Read both inputs and decode the JSON string into an events array. Create two DateTime objects: one for today's date (using the second input) and one for each event's date as you iterate through the events.

Filter and display only the upcoming events - events whose date is greater than or equal to today's date. For each upcoming event, print it in the following format:

Upcoming: [name] on [date] at [location]

Print each upcoming event on a separate line, in the order they appear in the array. If an event's date is before today's date, do not print it.

Input format:

  • First line: A JSON string representing the events array with keys name, date, and location
  • Second line: A date string in YYYY-MM-DD format representing today's date (example: 2024-04-01)

Expected output: Each upcoming event printed on a separate line in the format: Upcoming: [name] on [date] at [location]

Try it yourself

<?php
// Read the JSON string of events
$eventsJson = fgets(STDIN);
$events = (array)json_decode($eventsJson, true);

// Read today's date
$todayDate = trim(fgets(STDIN));

// TODO: Write your code below
// Create DateTime object for today's date
// Loop through events and compare dates
// Print upcoming events in the required format

?>

All lessons in Logic & Flow